457,704 Members | 1,270 Online Need help? Post your question and get tips & solutions from a community of 457,704 IT Pros & Developers. It's quick & easy.

# Random numbers

 P: n/a I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num ) { // return a uniformly distributed random number R in the range: 0 <= R <= (num-1) } I also would like this to be as executionally fast as possible. I have been trying to to this using rand() as my basis, but everything I try ends up with one problem or another (e.g., outside of range, not evenly distributed, execution too long). I'm using VS C++ 2005 Express Edition (managed /clr pure), and was hoping that there is a better alternative to rand( ) as the basis of random numbers, since this always generates an evenly distributed number from 0 to RAND_MAX (0x7fff), which is not easily adapted to the task I've described above... If there is a cheap third-party class I could buy to do this, I'd be interested in that too... Thanx in advance for responses! :) Jun 27 '07 #1
13 Replies

 P: n/a OK, I'm an idiot (feel free to throw stones....hehe). Here I am looking for some class in .NET that produces random numbers, and it never occurs to me to look for a class named 'Random'. Which, of course, exists; and, of course, does exactly what I want! :) "Peter Oliphant" I would like to be able to create a random number generator that producesevenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000, or between 0 and 99999 (inclusive). Further, the I want the range to be a variable. Concretely, I would like to create the following method: unsigned long Random( unsigned long num ) { // return a uniformly distributed random number R in the range: 0 <= R <= (num-1) } I also would like this to be as executionally fast as possible. I have been trying to to this using rand() as my basis, but everything I try ends up with one problem or another (e.g., outside of range, not evenly distributed, execution too long). I'm using VS C++ 2005 Express Edition (managed /clr pure), and was hoping that there is a better alternative to rand( ) as the basis of random numbers, since this always generates an evenly distributed number from 0 to RAND_MAX (0x7fff), which is not easily adapted to the task I've described above... If there is a cheap third-party class I could buy to do this, I'd be interested in that too... Thanx in advance for responses! :) Jun 27 '07 #2

 P: n/a "Peter Oliphant" I would like to be able to create a random number generator that producesevenly distributed random numbers up to given number. A quick, though, not perfect, method is to compute a random number with rand() and then take the remainder of a division. e.g. rand() % 10 yields numbers in {0 ... 9}. if you want {1 ... 10} add 1. Of course, since the highest number rand() returns is RAND_MAX and that is not evenly divisible by 10, your numbers won't have an even distribution. Naively, I'd suggest in that case that you pick your own maximum such that it is evenly divisble by the size of the range in question and discard anything above it. If you need numbers larger than RAND_MAX you can put two 16 bit numbers together to make a 32 bit number. See the MAKELONG macro for example. Of course, this is the kind of problem that has no doubt been faced and solved thousands of times, so if you need a "good" random number generator I suggest that you search for an algorithm or make friends with a PhD in math. ;-) Regards, Will www.ivrforbeginners.com Jun 27 '07 #3

 P: n/a Hi William, Thanx! Yeah, I know of the method you speak. There are, in fact two basic methods I tried, but each fails my 'criteria' for one reason or another. For the purposes of the dicussion below, the range of numbers to find a random number in is 0 to (max-1). The method you mentioned, ala getting a big random number and then modulo'ing it with the 'max' value, does always return a random number in range and does span the whole range, but unless the 'max' is a integral divisor of RAND_MAX (which means it has to be a power of 2) the results will not be evenly distributed (ala, the lower numbers will be produced more than the upper numbers). The other basic method is to keep getting big random numbers until one falls in range, rejecting any out of range. As long as the big random numbers can range from 0 to outside 'max' (and are evely distributed themselves), this does indeed pick an evenly distributed random number in range, and does span the range. But it is execution-time VERY non-deterministic, and in fact can take a very long time (computer exectution-wise) to deal with some 'max' values. For example, trying to get a result of 0 or 1 (i.e., max = 2) and using big random numbers up to 64K will find one in range on average in only 1 in 32K tries, and thus on average would take many failed iterations to eventually get one in range. Thus I tried separating cases based on 'max', such as masking a big random number with 0xF for 'max's less than 16. This works, but one ends up with many many many case, and it gets so complex, the mechanism to figure out which method to use becomes intrusive. Thus, the solution I found was a cheat....sort of....hehe...it turns out there exists a Random class that has a Next(max) method that produces a non-negative random number less than 'max', where 'max' can be 32-bits in size (i.e., exactly what I'm looking for!!!)... I've impleneted it, but have yet to see if the MS class is indeed uniformly distributed. I will do tests to be sure, but I was VERY glad to find this class! [==P==] "William DePalo [MVP VC++]" >I would like to be able to create a random number generator that producesevenly distributed random numbers up to given number. A quick, though, not perfect, method is to compute a random number with rand() and then take the remainder of a division. e.g. rand() % 10 yields numbers in {0 ... 9}. if you want {1 ... 10} add 1. Of course, since the highest number rand() returns is RAND_MAX and that is not evenly divisible by 10, your numbers won't have an even distribution. Naively, I'd suggest in that case that you pick your own maximum such that it is evenly divisble by the size of the range in question and discard anything above it. If you need numbers larger than RAND_MAX you can put two 16 bit numbers together to make a 32 bit number. See the MAKELONG macro for example. Of course, this is the kind of problem that has no doubt been faced and solved thousands of times, so if you need a "good" random number generator I suggest that you search for an algorithm or make friends with a PhD in math. ;-) Regards, Will www.ivrforbeginners.com Jun 27 '07 #4

 P: n/a In article , Peter Oliphant I would like to be able to create a random number generator thatproduces evenly distributed random numbers up to given number. Computer-generated random numbers tend to not be very good, unless you REALLY know what you're doing. rand() in the C standard library tends to be barely usable-- on a lot of systems, it only has 16-bit accuracy. You really should stick with finding a better random number generator and use it -- the Mersenne Twister from http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html has gotten a bunch of good reviews, and is quite free (BSD license, which basically says "feel free to use it, even in commercial apps." That site has C code available, and allows you to get a random # up to a range. Nathan Mates -- <*Nathan Mates - personal webpage http://www.visi.com/~nathan/ # Programmer at Pandemic Studios -- http://www.pandemicstudios.com/ # NOT speaking for Pandemic Studios. "Care not what the neighbors # think. What are the facts, and to how many decimal places?" -R.A. Heinlein Jun 27 '07 #5

 P: n/a Hi, Yeah, I know of the method you speak. There are, in fact two basic methods I tried, but each fails my 'criteria' for one reason or another. For the purposes of the dicussion below, the range of numbers to find a random number in is 0 to (max-1). The method you mentioned, ala getting a big random number and then modulo'ing it with the 'max' value, does always return a random number in range and does span the whole range, but unless the 'max' is a integral divisor of RAND_MAX (which means it has to be a power of 2) the results will not be evenly distributed (ala, the lower numbers will be produced more than the upper numbers). The other basic method is to keep getting big random numbers until one falls in range, rejecting any out of range. As long as the big random numbers can range from 0 to outside 'max' (and are evely distributed themselves), this does indeed pick an evenly distributed random number in range, and does span the range. But it is execution-time VERY non-deterministic Another one would be long nDiv = RAND_MAX / yourMax; long n = (rand() / nDiv) % yourMax; So if RAND_MAX == 100 and you want 0..9 then you get 0..9 as 0 and 10..19 as 1 and so on. You might get yourMax as result so I added the modulo operation to address that. it turns out there exists a Random class that has a Next(max) method that produces a non-negative random number less than 'max', where 'max' can be 32-bits in size (i.e., exactly what I'm looking for!!!)... I've impleneted it, but have yet to see if the MS class is indeed uniformly distributed. I will do tests to be sure, but I was VERY glad to find this class! Im curious of your findings. -- SvenC Jun 27 '07 #6

 P: n/a On Wed, 27 Jun 2007 09:10:27 -0700, "Peter Oliphant" The other basic method is to keep getting big random numbers until one fallsin range, rejecting any out of range. As long as the big random numbers canrange from 0 to outside 'max' (and are evely distributed themselves), thisdoes indeed pick an evenly distributed random number in range, and does spanthe range. But it is execution-time VERY non-deterministic, and in fact cantake a very long time (computer exectution-wise) to deal with some 'max'values. For example, trying to get a result of 0 or 1 (i.e., max = 2) andusing big random numbers up to 64K will find one in range on average in only1 in 32K tries, and thus on average would take many failed iterations toeventually get one in range. You don't need to do that much iteration, or for max = 2 with odd RAND_MAX, any at all. See: http://c-faq.com/lib/randrange.html -- Doug Harrison Visual C++ MVP Jun 27 '07 #7

 P: n/a On Wed, 27 Jun 2007 18:27:18 +0200, "SvenC" Another one would belong nDiv = RAND_MAX / yourMax;long n = (rand() / nDiv) % yourMax;So if RAND_MAX == 100 and you want 0..9 then you get 0..9 as 0 and 10..19 as1 and so on.You might get yourMax as result so I added the modulo operation to addressthat. That produces an uneven distribution a lot of the time. See the link to the C FAQ I posted in my reply to Peter for an approach that doesn't. (To see the problem in your approach, consider RAND_MAX = 100 and yourMax = 99. Then nDiv is 1, rand()/nDiv is 0..99, and 99%99 is 0. So you will have 0 appearing twice as often as any other number.) -- Doug Harrison Visual C++ MVP Jun 27 '07 #8

 P: n/a Doug Harrison [MVP] wrote: On Wed, 27 Jun 2007 18:27:18 +0200, "SvenC" wrote: >Another one would belong nDiv = RAND_MAX / yourMax;long n = (rand() / nDiv) % yourMax;So if RAND_MAX == 100 and you want 0..9 then you get 0..9 as 0 and10..19 as 1 and so on.You might get yourMax as result so I added the modulo operation toaddress that. That produces an uneven distribution a lot of the time. See the link to the C FAQ I posted in my reply to Peter for an approach that doesn't. (To see the problem in your approach, consider RAND_MAX = 100 and yourMax = 99. Then nDiv is 1, rand()/nDiv is 0..99, and 99%99 is 0. So you will have 0 appearing twice as often as any other number.) Yes, figured that out after thinking again about my post. I'll have a look at the FAQ. -- SvenC Jun 27 '07 #9

 P: n/a Keep in mind, my desire is to produce random rumber is a ranges GREATER than RAND_MAX, and that the range be sometihng I can establish on the fly (i.e., it's a variable). I did experiements on the innate Random class, and it works just fine. It can be seeded (I use current 'time'), and can range from 0 to ANY number up to 31 bits (the return value is signed). 31-bits gets me ranges up to 2 billion or so, which is (more than) just fine for my purposes! : ) All that is required is the following: Random^ R = gcnew Random( /* int seed if desired*/ ) ; int r = R->Next( max ) ; // 0 <= r < max [==P==] "Doug Harrison [MVP]" >The other basic method is to keep getting big random numbers until onefallsin range, rejecting any out of range. As long as the big random numberscanrange from 0 to outside 'max' (and are evely distributed themselves), thisdoes indeed pick an evenly distributed random number in range, and doesspanthe range. But it is execution-time VERY non-deterministic, and in factcantake a very long time (computer exectution-wise) to deal with some 'max'values. For example, trying to get a result of 0 or 1 (i.e., max = 2) andusing big random numbers up to 64K will find one in range on average inonly1 in 32K tries, and thus on average would take many failed iterations toeventually get one in range. You don't need to do that much iteration, or for max = 2 with odd RAND_MAX, any at all. See: http://c-faq.com/lib/randrange.html -- Doug Harrison Visual C++ MVP Jun 27 '07 #10

 P: n/a Peter Oliphant wrote: The other basic method is to keep getting big random numbers until one falls in range, rejecting any out of range. As long as the big random numbers can range from 0 to outside 'max' (and are evely distributed themselves), this does indeed pick an evenly distributed random number in range, and does span the range. But it is execution-time VERY non-deterministic, and in fact can take a very long time (computer exectution-wise) to deal with some 'max' values. For example, trying to get a result of 0 or 1 (i.e., max = 2) and using big random numbers up to 64K will find one in range on average in only 1 in 32K tries, and thus on average would take many failed iterations to eventually get one in range. I would say this method is not guaranteed to ever finish. ... Jun 27 '07 #11

 P: n/a shadowman http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too Jun 27 '07 #12

 P: n/a "shadowman" The other basic method is to keep getting big random numbers until onefalls in range, rejecting any out of range. As long as the big randomnumbers can range from 0 to outside 'max' (and are evely distributedthemselves), this does indeed pick an evenly distributed random number inrange, and does span the range. But it is execution-time VERYnon-deterministic, and in fact can take a very long time (computerexectution-wise) to deal with some 'max' values. For example, trying toget a result of 0 or 1 (i.e., max = 2) and using big random numbers up to64K will find one in range on average in only 1 in 32K tries, and thus onaverage would take many failed iterations to eventually get one in range. I would say this method is not guaranteed to ever finish. ... Actually, if the generator of the big random numbers is a truly evenly distributed one, then it is guaranteed to EVENTUALLY (i.e., in FINTE time) produce a value in any sub-range it spans. This would include a range from 0 to any value smaller than its max. Hence, it is GUARANTEED to finish in finite time, but like I said, in NON_DETERMINISTIC finite time. It could happen the first attempt, and there is no number of attempts for which it will definitiely happen within. But as the number of attempts increase, the probability of continued failure goes to zero (viewed as a probability over the entire history, not individual attempt probability, which is assumed in this example to be fixed). Specifically, the probability it will happen in the first N attempts is: p = 1 - (1 - (1/32K)) ^ N That is, the compliment of the probability it WON'T happen (which is the compliment of it happening) in N attempts (which is the product of the probabilities of the individual attempts failing, hence the exponent of N). Note that as N gets larger the second term goes to zero, so the probability of succes goes to 1... [==P==] Jun 28 '07 #13

 P: n/a "Peter Oliphant" = N * M, try again Now the probability of needing to retry is maximum at N = (RAND_MAX+1)/2 for odd RAND_MAX and is never greater than 0.5 for any N. Cache the computed value of N*M for efficiency. > Thus I tried separating cases based on 'max', such as masking a big random number with 0xF for 'max's less than 16. This works, but one ends up with many many many case, and it gets so complex, the mechanism to figure out which method to use becomes intrusive. Thus, the solution I found was a cheat....sort of....hehe...it turns out there exists a Random class that has a Next(max) method that produces a non-negative random number less than 'max', where 'max' can be 32-bits in size (i.e., exactly what I'm looking for!!!)... I've impleneted it, but have yet to see if the MS class is indeed uniformly distributed. I will do tests to be sure, but I was VERY glad to find this class! [==P==] "William DePalo [MVP VC++]" "Peter Oliphant" >>I would like to be able to create a random number generator that producesevenly distributed random numbers up to given number. A quick, though, not perfect, method is to compute a random number withrand() and then take the remainder of a division. e.g. rand() % 10yields numbers in {0 ... 9}. if you want {1 ... 10} add 1.Of course, since the highest number rand() returns is RAND_MAX and thatis not evenly divisible by 10, your numbers won't have an evendistribution.Naively, I'd suggest in that case that you pick your own maximum suchthat it is evenly divisble by the size of the range in question anddiscard anything above it.If you need numbers larger than RAND_MAX you can put two 16 bit numberstogether to make a 32 bit number. See the MAKELONG macro for example.Of course, this is the kind of problem that has no doubt been faced andsolved thousands of times, so if you need a "good" random numbergenerator I suggest that you search for an algorithm or make friends witha PhD in math. ;-)Regards,Willwww.ivrforbeginners.com Jun 29 '07 #14

### This discussion thread is closed

Replies have been disabled for this discussion. 