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 <= (num1)
}
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 thirdparty class I could buy to do this, I'd be
interested in that too...
Thanx in advance for responses! :)  
Share this Question
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" <po*******@RoundTripInc.comwrote in message
news:uW**************@TK2MSFTNGP02.phx.gbl...
>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 <= (num1)
}
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 thirdparty class I could buy to do this, I'd be
interested in that too...
Thanx in advance for responses! :)  
P: n/a

"Peter Oliphant" <po*******@RoundTripInc.comwrote in message
news:uW**************@TK2MSFTNGP02.phx.gbl...
>I would like to be able to create a random number generator that produces evenly 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  
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 (max1).
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 executiontime VERY nondeterministic, and in fact can
take a very long time (computer exectutionwise) 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
nonnegative random number less than 'max', where 'max' can be 32bits 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++]" <wi***********@mvps.orgwrote in message
news:uj**************@TK2MSFTNGP02.phx.gbl...
"Peter Oliphant" <po*******@RoundTripInc.comwrote in message
news:uW**************@TK2MSFTNGP02.phx.gbl...
>>I would like to be able to create a random number generator that produces evenly 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
 
P: n/a

In article <uW**************@TK2MSFTNGP02.phx.gbl>,
Peter Oliphant <po*******@RoundTripInc.comwrote:
>I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number.
Computergenerated 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 16bit
accuracy. You really should stick with finding a better random number
generator and use it  the Mersenne Twister from http://www.math.sci.hiroshimau.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  
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 (max1).
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
executiontime VERY nondeterministic
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 nonnegative random number less than 'max', where 'max'
can be 32bits 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  
P: n/a

On Wed, 27 Jun 2007 09:10:27 0700, "Peter Oliphant"
<po*******@RoundTripInc.comwrote:
>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 executiontime VERY nondeterministic, and in fact can take a very long time (computer exectutionwise) 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.
You don't need to do that much iteration, or for max = 2 with odd RAND_MAX,
any at all. See: http://cfaq.com/lib/randrange.html

Doug Harrison
Visual C++ MVP  
P: n/a

On Wed, 27 Jun 2007 18:27:18 +0200, "SvenC" <Sv***@community.nospamwrote:
>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.
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  
P: n/a

Doug Harrison [MVP] wrote:
On Wed, 27 Jun 2007 18:27:18 +0200, "SvenC" <Sv***@community.nospam>
wrote:
>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.
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  
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). 31bits 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]" <ds*@mvps.orgwrote in message
news:1f********************************@4ax.com...
On Wed, 27 Jun 2007 09:10:27 0700, "Peter Oliphant"
<po*******@RoundTripInc.comwrote:
>>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 executiontime VERY nondeterministic, and in fact can take a very long time (computer exectutionwise) 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.
You don't need to do that much iteration, or for max = 2 with odd
RAND_MAX,
any at all. See:
http://cfaq.com/lib/randrange.html

Doug Harrison
Visual C++ MVP
 
P: n/a

Peter Oliphant wrote:
<snipped>
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 executiontime VERY nondeterministic, and in fact can
take a very long time (computer exectutionwise) 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.
...
 
P: n/a

shadowman <sh*******@noemail.comwrote:
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 executiontime VERY nondeterministic, and in fact can
take a very long time (computer exectutionwise) 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.
If the numbers are evenly distributed, then it's guaranteed that each
number within the range will occur *eventually*, isn't it?
Put it this way  if it's a PRNG, it will have a cycle eventually. If
it cycles without ever hitting a particular number, the distribution
cannot be said to be even.
Therefore so long as at least one number within the "target" range is
within the range of the PRNG, it will finish at some point.
If it's a truly random number generator that makes things harder...

Jon Skeet  <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too  
P: n/a

"shadowman" <sh*******@noemail.comwrote in message
news:f5**********@aioe.org...
Peter Oliphant wrote:
><snipped>
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 executiontime VERY nondeterministic, and in fact can take a very long time (computer exectutionwise) 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.
...
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 subrange 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==]  
P: n/a

"Peter Oliphant" <po*******@RoundTripInc.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
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 (max1).
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 executiontime VERY
nondeterministic, and in fact can take a very long time (computer
exectutionwise) 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.
Combine these two ideas.
If you desire a random number [0, N):
Let M = RAND_MAX / N (integral division)
Now, if rand() returns either a number less than N * M, take the modulus,
resulting in an even distribution
if rand() returns >= 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
nonnegative random number less than 'max', where 'max' can be 32bits 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++]" <wi***********@mvps.orgwrote in message
news:uj**************@TK2MSFTNGP02.phx.gbl...
>"Peter Oliphant" <po*******@RoundTripInc.comwrote in message news:uW**************@TK2MSFTNGP02.phx.gbl...
>>>I would like to be able to create a random number generator that produces evenly 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
  This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 2140
 replies: 13
 date asked: Jun 27 '07
