How can I generate a random whole number between two specified variables in Javascript, e.g. x = 4 and y = 8 would output any of 4, 5, 6, 7, 8?
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
|||||||||||||||||||||
|
|
There are some examples on the Mozilla Developer Network page:
Here's the logic behind it. It's a simple rule of three:
Now, we'd like a number between
We can use the
This gives:
We may now apply
So, in order to find
Don't forget to add
That was the first function from MDN. The second one, returns an integer between Now for getting integers, you could use You could use
With With
You can't use |
|||||||||||||||||||||
|
|
|||||||||||||||||||||
|
Math.random()From the Mozilla Developer Network documentation:
Useful examples:
|
||||
|
|
usage:
breakdown: We are returning a function (borrowing from functional programming) that when called, will return a random integer between the the values (bottom is lower number, top is greater number)
The code in the previous example gave us an integer in the range NOTE: If you pass in a non-integer value or the greater number first you'll get undesirable behavior, but unless anyone requests it I am not going to delve into the argument checking code as its rather far from the intent of the original question. |
|||||||||||||||||||||
|
|
Return a random number between 1 and 10:
Return a random number between 1 and 100:
|
||||
Alternative if you are using Underscore.js you can use
|
|||||||||||||||||||||
|
|
The other answers don't account for the perfectly reasonable parameters of
|
|||||
|
|
After generating a random number using a computer program, it is still consider as a random number if the picked number is a part or the full one of the initial one. But if it was changed, then mathematicians are not accept it as a random number and they can call it a biased number. But if you are developing a program for a simple task, this will not be a case to consider. But if you are developing a program to generate a random number for a valuable stuff such as lottery program, or gambling game, then your program will be rejected by the management if you are not consider about the above case. So for those kind of people, here is my suggestion: Generate a random number using
If you know how to read random number table to pick a random number, you know above process(multiplying by 1, 10, 100 and so on) is not violates the one that I was mentioned at the beginning.( Because it changes only the place of the decimal point.) Study the following example and develop it to your needs. If you need a sample [0,9] then floor of n*10 is your answer and if need [0,99] then floor of n*100 is your answer and so on. Now let enter into your role: You've asked numbers among specific range. (In this case you are biased among that range. - By taking a number from [1,6] by roll a die, then you are biased into [1,6] but still it is a random if and only if die is unbiased.) So consider your range ==> [78, 247] number of elements of the range = 247 - 78 + 1 = 170; (since both the boundaries are inclusive.
Note: In method one, first I created an array which contains numbers that you need and then randomly put them into another array. In method two, generate numbers randomly and check those are in the range that you need. Then put it into an array. Here I generated two random numbers and used total of them to maximize the speed of the program by minimizing the failure rate that obtaining a useful number. However adding generated numbers will also give some biassness. So I would recommend my first method to generate random numbers within a specific range. In both methods, your console will show the result.(Press f12 in Chrome to open the console) |
|||||||||||||||||
|
|
For a random integer with a range, try:
|
|||||
|
|
To get a random number say between 1 and 6, first do:
This multiplies a random number by 6 and then adds 0.5 to it. Next round the number to a positive integer by doing:
This round the number to the nearest whole number. Or to make it more understandable do this:
In general the code for doing this using variables is:
The reason for taking away 0.5 from the minimum value is because using the minimum value alone would allow you to get an integer that was one more than your maximum value. By taking away 0.5 from the minimum value you are essentially preventing the maximum value from being rounded up. Hope that helps. |
|||||
|
|
Using following code you can generate array of random numbers, without repeating, in a given range.
|
|||
|
|
|
Here is the MS DotNet Implementation of Random class in javascript-
Use:
|
|||
|
|
|
Random whole number between lowest and highest:
Not the most elegant solution.. but something quick. |
|||
|
|
Uint8Array create a array filled with a number up to 3 digits which would be a maximum of 999. This code is very short. |
|||
|
|
|
Here's what I use to generate random numbers.
We do execute
|
|||||
|
|
this is my take on a random number in a range, as in I wanted to get a random number within a range of base to exponent. e.g. base = 10, exponent = 2, gives a random number from 0 to 100, ideally, and so on. if it helps use it, here it is:
|
|||
|
|
|
I know this question is already answered but my answer could help someone. I found this simple method on W3Schools:
Hope this would help someone. |
|||||||||||||
|
To test this function, and variations of this function, save the below HTML/JavaScript to a file and open with a browser. The code will produce a graph showing the distribution of one million function calls. The code will also record the edge cases, so if the the function produces a value greater than the max, or less than the min, you.will.know.about.it.
|
||||
|
|
|
This can handle generating upto 20 digit UNIQUE random number JS
|
||||
|
|
|
If you need variable betwee 0 and max you can use:
|
|||
|
|
|
Hey Guys I have created a javascript function Math.random() which we can generate random number of specified length. Without using any INBUILT function.
|
||||
|
|
protected by Engineer Oct 23 '13 at 14:42
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
