Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8393725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:52:33+00:00 2026-06-09T19:52:33+00:00

I wanted some information about generating random integers. I look for it in Google

  • 0

I wanted some information about generating random integers. I look for it in Google and Stack Overflow and mostly found code like this (in case we want numbers from 1 to 52):

var randInt=function(){
   number=Math.floor(Math.random()*52+1);
};

and

var randNumMin = 1;
var randNumMax = 52;
var randInt = function (){
   number = (Math.floor(Math.random() * (randNumMax - randNumMin + 1)) + randNumMin);
};

I read some references about Math.random and found that it is generating numbers from 0 to 1. In case Math.random generates 1, we will get number 5, so it means we will get error. I agree that it is very rare case, but it is possible. I slightly modified code to avoid that error (in our case generation of number 53). Here I think is a right code for generation random numbers in JavaScript. In your examples it generates only integers but I think it is possible to modify code and generate any kind of number:

var randInt = function(){
    number = Math.floor(Math.random()*52+1);
    if (number === 53){
       randInt();
    }
};

and

var randNumMin = 1;
var randNumMax = 52;
var randInt = function (){
    number = (Math.floor(Math.random() * (randNumMax - randNumMin + 1)) + randNumMin);
    if (number===53){
        randInt();
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:52:35+00:00Added an answer on June 9, 2026 at 7:52 pm

    The Math.random() function generates random numbers x where 0 <= x < 1. So it will never generate exactly 1, although it might come really close.

    From the documentation for random:

    Returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to do some research but i could not find any information about
I would like to learn ASP.NET and just wanted some input as to which
I have class MasterData whose instances contain some general information about all possible gadgets.
I went through few blogs and sites which gave me some information about how
I was looking for some technical information about how RequestInterceptor from the WCF REST
I just wanted some input about my use of JSON. <?php header('Content-Type: text/plain'); //results
Every time I try to get some information about my video files with ffmpeg,
I have a div on a page that shows some information about a particular
I wanted some system where I didn't have to keep looking at the Exception
I know the topic I started is too subjective. But I just wanted some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.