Trying to think of a number but just can’t come up with anything? Demonstrating probability for a classroom? Need to pull a random number from a hat for a raffle? You’ve come to the right place! Our random number generator will give you just that—a random number. A random number is a number chosen by chance from a set range. The
beauty of random numbers is that you can’t predict what number you’ll get. Even if you choose 2 the first time, 4 the second, and 6 the third, any perceived pattern is totally random! The fourth time you choose it could be 19 or 100. It’s all up to chance. Our generator will provide a random number between the two numbers of your choice. Just enter a lower limit number and an upper limit number and click ENTER. Your random number will be generated and appear in the
box. Now, if you're trying to come up with a list of random numbers yourself, you probably will find a pattern. Even subconsciously, the human mind is conditioned to look for and create things in a pattern or sequence. Unfortunately, this could negatively impact math problems you're working on or activities you have planned. To make sure it stays up to chance and that you don’t have any influence over
this choice, we’ve created this handy random number generator that can select a number for you. It’s simple to use. Just put in two numbers and the random number generator will give you an integer in between them. The random number generator recognizes whole numbers and negative numbers. It does not recognize decimals, fractions, or equations. If you insert a percent symbol, the symbol will not carry over into the random number
box. Let's say you need to pull a random number for a raffle
prize. There are thirty people who enter the raffle, and only three people will win. The tickets range from 1 to 150, so the Lower Limit is 1 and the Upper Limit would be 150. Click Enter and the random number generator gives you a 7, 36, and 2. Raffle participants with the numbers 7, 36, and 2 on their tickets will be the three raffle winners. Imagine you are trying to find a number between 2.01 and 2.5. You enter
2.01 as your Lower Limit and 2.5 as your Upper Limit. You click Enter, and you get—a 2? That’s not between your values! Sorry, there are no decimals allowed in this random number generator. However, as long as you're working within decimals of the same whole number, you could use other whole numbers as your two values, and assign the result after the decimal. Generate An Instant Random Number Between Two Numbers Of Your Choice.
Why You Should Use a Random Number Generator
How To Use a Random Number Generator
Working Example
Decimal Trouble
So if your range is between 2.01 and 2.5, you could enter 1 and 50 in the value boxes. Let's say it generates the number 25. You could assign that after your decimal, and this would be your result: 2.25
Are you trying to calculate a problem or convert other numbers between units? Check out our Other Calculators!
Sign Up For Our FREE Newsletter!
Sign Up For Our FREE Newsletter!
I think I have four answers, two giving exact solutions like that of @Adam Rosenfield but without the infinite loop problem, and other two with almost perfect solution but faster implementation than first one.
The best exact solution requires 7 calls to rand5, but lets proceed in order to understand.
Method 1 - Exact
Strength of Adam's answer is that it gives a perfect uniform distribution, and there is very high probability (21/25) that only two calls to rand5() will be needed. However, worst case is infinite loop.
The first solution below also gives a perfect uniform distribution, but requires a total of 42 calls to rand5. No infinite loops.
Here is an R implementation:
For people not familiar with R, here is a simplified version:
rand7 = function(){ r = 0 for(i in 0:6){ r = r + i + rand5() + rand5()*2 + rand5()*3 + rand5()*4 + rand5()*5 + rand5()*6 } return r %% 7 + 1 }The distribution of rand5 will be preserved. If we do the math, each of the 7 iterations of the loop has 5^6 possible combinations, thus total number of possible combinations are (7 * 5^6) %% 7 = 0. Thus we can divide the random numbers generated in equal groups of 7. See method two for more discussion on this.
Here are all the possible combinations:
table(apply(expand.grid(c(outer(1:5,0:6,"+")),(1:5)*2,(1:5)*3,(1:5)*4,(1:5)*5,(1:5)*6),1,sum) %% 7 + 1) 1 2 3 4 5 6 7 15625 15625 15625 15625 15625 15625 15625I think it's straight forward to show that Adam's method will run much much faster. The probability that there are 42 or more calls to rand5 in Adam's solution is very small ((4/25)^21 ~ 10^(-17)).
Method 2 - Not Exact
Now the second method, which is almost uniform, but requires 6 calls to rand5:
rand7 <- function() (sum(sapply(1:6,function(i) i*rand5())) %% 7) + 1Here is a simplified version:
rand7 = function(){ r = 0 for(i in 1:6){ r = r + i*rand5() } return r %% 7 + 1 }This is essentially one iteration of method 1. If we generate all possible combinations, here is resulting counts:
table(apply(expand.grid(1:5,(1:5)*2,(1:5)*3,(1:5)*4,(1:5)*5,(1:5)*6),1,sum) %% 7 + 1) 1 2 3 4 5 6 7 2233 2232 2232 2232 2232 2232 2232One number will appear once more in 5^6 = 15625 trials.
Now, in Method 1, by adding 1 to 6, we move the number 2233 to each of the successive point. Thus the total number of combinations will match up. This works because 5^6 %% 7 = 1, and then we do 7 appropriate variations, so (7 * 5^6 %% 7 = 0).
Method 3 - Exact
If the argument of method 1 and 2 is understood, method 3 follows, and requires only 7 calls to rand5. At this point, I feel this is the minimum number of calls needed for an exact solution.
Here is an R implementation:
rand5 <- function() sample(1:5,1) rand7 <- function() (sum(sapply(1:7, function(i) i * rand5())) %% 7) + 1For people not familiar with R, here is a simplified version:
rand7 = function(){ r = 0 for(i in 1:7){ r = r + i * rand5() } return r %% 7 + 1 }The distribution of rand5 will be preserved. If we do the math, each of the 7 iterations of the loop has 5 possible outcomes, thus total number of possible combinations are (7 * 5) %% 7 = 0. Thus we can divide the random numbers generated in equal groups of 7. See method one and two for more discussion on this.
Here are all the possible combinations:
table(apply(expand.grid(0:6,(1:5)),1,sum) %% 7 + 1) 1 2 3 4 5 6 7 5 5 5 5 5 5 5I think it's straight forward to show that Adam's method will still run faster. The probability that there are 7 or more calls to rand5 in Adam's solution is still small ((4/25)^3 ~ 0.004).
Method 4 - Not Exact
This is a minor variation of the the second method. It is almost uniform, but requires 7 calls to rand5, that is one additional to method 2:
rand7 <- function() (rand5() + sum(sapply(1:6,function(i) i*rand5())) %% 7) + 1Here is a simplified version:
rand7 = function(){ r = 0 for(i in 1:6){ r = r + i*rand5() } return (r+rand5()) %% 7 + 1 }If we generate all possible combinations, here is resulting counts:
table(apply(expand.grid(1:5,(1:5)*2,(1:5)*3,(1:5)*4,(1:5)*5,(1:5)*6,1:5),1,sum) %% 7 + 1) 1 2 3 4 5 6 7 11160 11161 11161 11161 11161 11161 11160Two numbers will appear once less in 5^7 = 78125 trials. For most purposes, I can live with that.