Random Number Generation | | |
Hi everybody,
May i know how to write a code that generates random numbers as many
times as one would want ? i.e. like the standard function 'rand()' or
may i get the basic logic behind it ?
Thanks in advance! | | | | re: Random Number Generation
muttaa said:
[color=blue]
> Hi everybody,
>
> May i know how to write a code that generates random numbers as many
> times as one would want ? i.e. like the standard function 'rand()' or
> may i get the basic logic behind it ?[/color]
One common technique is the Linear Congruential PseudoRandom Number
Generator. This works on the principle of taking a "current" pseudo-random
value r and three constants, a, c, and m, and producing the next
pseudo-random number by this method:
r = (a * r + c) % m;
How good this generator is depends largely on your choices for a, c, and m
(and, of course, other factors, such as the range of r).
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously) | | | | re: Random Number Generation
>May i know how to write a code that generates random numbers as many[color=blue]
>times as one would want ?[/color]
Code does not generate random numbers. Code generates pseudo-random
numbers. For real random numbers you need hardware.
Any pseudo-random number generator will repeat itself eventually, even
if it takes longer than the expected lifetime of the universe.
[color=blue]
>i.e. like the standard function 'rand()' or
>may i get the basic logic behind it ?[/color]
Google for "mersenne twister". Distributions for Linux and FreeBSD
also contain source code for various pseudo-random number generators
like random() and srand48().
Gordon L. Burditt | | | | re: Random Number Generation
Gordon Burditt said:
[color=blue][color=green]
>>May i know how to write a code that generates random numbers as many
>>times as one would want ?[/color]
>
> Code does not generate random numbers. Code generates pseudo-random
> numbers. For real random numbers you need hardware.[/color]
Or wetware. You can ask users to supply real random numbers, and use those.
Of course, they might just give you 1, 2, 3, 4, 5..., but that's a QofI
issue!
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously) | | | | re: Random Number Generation
"muttaa" <asgosa.eeegct@gmail.com> writes:[color=blue]
> May i know how to write a code that generates random numbers as many
> times as one would want ? i.e. like the standard function 'rand()' or
> may i get the basic logic behind it ?[/color]
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Question 13.15 is
"I need a random number generator."; questions 13.16 through 13.21
also deal with random numbers.
Start there, and come back if you still have questions.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this. | | | | re: Random Number Generation
muttaa wrote:[color=blue]
> Hi everybody,
>
> May i know how to write a code that generates random numbers as many
> times as one would want ? i.e. like the standard function 'rand()' or
> may i get the basic logic behind it ?
>
> Thanks in advance!
>[/color]
Google for Marsaglia.
--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia | | | | re: Random Number Generation
Richard Heathfield wrote:
[color=blue]
> muttaa said:
>[color=green]
>> Hi everybody,
>>
>> May i know how to write a code that generates random numbers as many
>> times as one would want ? i.e. like the standard function 'rand()' or
>> may i get the basic logic behind it ?[/color]
>
> One common technique is the Linear Congruential PseudoRandom Number
> Generator. This works on the principle of taking a "current" pseudo-random
> value r and three constants, a, c, and m, and producing the next
> pseudo-random number by this method:
>
> r = (a * r + c) % m;
>
> How good this generator is depends largely on your choices for a, c, and m
> (and, of course, other factors, such as the range of r).
>[/color]
interesting.
m would be rand_max-1 i think
a and c would be prime numbers maybe? (well, that was my choice)
and initial value of r would be the seed.
I played with this once i saw your post and it seems quite workable
as a pseudo random generator. Its also extremely easy to implement.
I love little tidbits like this!
Eric | | | | re: Random Number Generation
Eric wrote On 05/19/06 20:40,:[color=blue]
> Richard Heathfield wrote:
>
>[color=green]
>>muttaa said:
>>
>>[color=darkred]
>>>Hi everybody,
>>>
>>>May i know how to write a code that generates random numbers as many
>>>times as one would want ? i.e. like the standard function 'rand()' or
>>>may i get the basic logic behind it ?[/color]
>>
>>
>>One common technique is the Linear Congruential PseudoRandom Number
>>Generator. This works on the principle of taking a "current" pseudo-random
>>value r and three constants, a, c, and m, and producing the next
>>pseudo-random number by this method:
>>
>> r = (a * r + c) % m;
>>
>>How good this generator is depends largely on your choices for a, c, and m
>>(and, of course, other factors, such as the range of r).
>>[/color]
>
> interesting.
> m would be rand_max-1 i think
> a and c would be prime numbers maybe? (well, that was my choice)
> and initial value of r would be the seed.
> I played with this once i saw your post and it seems quite workable
> as a pseudo random generator. Its also extremely easy to implement.
> I love little tidbits like this![/color]
From your speculations about appropriate values for
m, a, and c, it is clear that you have no knowledge at
all about what they should be. You therefore deserve
the Bad Things that are likely to happen to you if you
decide to implement any such "little tidbit." Alas, it
is only too likely that you will code it into a program
someone else will use, thus inflicting your ignorance
upon innocent bystanders.
If you can't be bothered to do any research at all,
there's no hope. But in hope you can be persuaded to
lift a finger five times and make a few mouse clicks,
I offer one word: RANDU.
-- Eric.Sosman@sun.com | | | | re: Random Number Generation
Eric Sosman wrote:[color=blue]
> Eric wrote On 05/19/06 20:40,:[color=green]
>> Richard Heathfield wrote:[color=darkred]
>>> muttaa said:
>>>>
>>>> May i know how to write a code that generates random numbers as
>>>> many times as one would want ? i.e. like the standard function
>>>> 'rand()' or may i get the basic logic behind it ?
>>>
>>> One common technique is the Linear Congruential PseudoRandom
>>> Number Generator. This works on the principle of taking a
>>> "current" pseudo-random value r and three constants, a, c, and m,
>>> and producing the next pseudo-random number by this method:
>>>
>>> r = (a * r + c) % m;
>>>
>>> How good this generator is depends largely on your choices for a,
>>> c, and m (and, of course, other factors, such as the range of r).[/color]
>>
>> interesting.
>> m would be rand_max-1 i think
>> a and c would be prime numbers maybe? (well, that was my choice)
>> and initial value of r would be the seed.
>> I played with this once i saw your post and it seems quite workable
>> as a pseudo random generator. Its also extremely easy to implement.
>> I love little tidbits like this![/color]
>
> From your speculations about appropriate values for
> m, a, and c, it is clear that you have no knowledge at
> all about what they should be. You therefore deserve
> the Bad Things that are likely to happen to you if you
> decide to implement any such "little tidbit." Alas, it
> is only too likely that you will code it into a program
> someone else will use, thus inflicting your ignorance
> upon innocent bystanders.
>
> If you can't be bothered to do any research at all,
> there's no hope. But in hope you can be persuaded to
> lift a finger five times and make a few mouse clicks,
> I offer one word: RANDU.[/color]
A better word might be TAOCP.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/> | | | | re: Random Number Generation
Eric Sosman wrote:
[color=blue]
>
>
> Eric wrote On 05/19/06 20:40,:[color=green]
>> Richard Heathfield wrote:
>>
>>[color=darkred]
>>>muttaa said:
>>>
>>>
>>>>Hi everybody,
>>>>
>>>>May i know how to write a code that generates random numbers as many
>>>>times as one would want ? i.e. like the standard function 'rand()' or
>>>>may i get the basic logic behind it ?
>>>
>>>
>>>One common technique is the Linear Congruential PseudoRandom Number
>>>Generator. This works on the principle of taking a "current"
>>>pseudo-random value r and three constants, a, c, and m, and producing the
>>>next pseudo-random number by this method:
>>>
>>> r = (a * r + c) % m;
>>>
>>>How good this generator is depends largely on your choices for a, c, and
>>>m (and, of course, other factors, such as the range of r).
>>>[/color]
>>
>> interesting.
>> m would be rand_max-1 i think
>> a and c would be prime numbers maybe? (well, that was my choice)
>> and initial value of r would be the seed.
>> I played with this once i saw your post and it seems quite workable
>> as a pseudo random generator. Its also extremely easy to implement.
>> I love little tidbits like this![/color]
>
> From your speculations about appropriate values for
> m, a, and c, it is clear that you have no knowledge at
> all about what they should be. You therefore deserve
> the Bad Things that are likely to happen to you if you
> decide to implement any such "little tidbit." Alas, it
> is only too likely that you will code it into a program
> someone else will use, thus inflicting your ignorance
> upon innocent bystanders.
>
> If you can't be bothered to do any research at all,
> there's no hope. But in hope you can be persuaded to
> lift a finger five times and make a few mouse clicks,
> I offer one word: RANDU.
>[/color]
Jesus Man, take a fucking chill pill will ya? Its not the end of the world
you know.
Eric | | | | re: Random Number Generation
Eric schrieb:[color=blue]
> Eric Sosman wrote:[color=green]
>>Eric wrote On 05/19/06 20:40,:[color=darkred]
>>>Richard Heathfield wrote:
>>>>muttaa said:
>>>>>
>>>>>May i know how to write a code that generates random numbers as many
>>>>>times as one would want ? i.e. like the standard function 'rand()' or
>>>>>may i get the basic logic behind it ?
>>>>
>>>>One common technique is the Linear Congruential PseudoRandom Number
>>>>Generator. This works on the principle of taking a "current"
>>>>pseudo-random value r and three constants, a, c, and m, and producing the
>>>>next pseudo-random number by this method:
>>>>
>>>> r = (a * r + c) % m;
>>>>
>>>>How good this generator is depends largely on your choices for a, c, and
>>>>m (and, of course, other factors, such as the range of r).
>>>
>>>interesting.
>>> m would be rand_max-1 i think
>>> a and c would be prime numbers maybe? (well, that was my choice)
>>> and initial value of r would be the seed.
>>>I played with this once i saw your post and it seems quite workable
>>>as a pseudo random generator. Its also extremely easy to implement.
>>>I love little tidbits like this![/color]
>>
>> From your speculations about appropriate values for
>>m, a, and c, it is clear that you have no knowledge at
>>all about what they should be. You therefore deserve
>>the Bad Things that are likely to happen to you if you
>>decide to implement any such "little tidbit." Alas, it
>>is only too likely that you will code it into a program
>>someone else will use, thus inflicting your ignorance
>>upon innocent bystanders.
>>
>> If you can't be bothered to do any research at all,
>>there's no hope. But in hope you can be persuaded to
>>lift a finger five times and make a few mouse clicks,
>>I offer one word: RANDU.[/color]
>
> Jesus Man, take a fucking chill pill will ya? Its not the end of the world
> you know.[/color]
Well, if your PRNG based on the above is used, the outcome
may be severely skewed -- you do not generate RAND_MAX-1 and
RAND_MAX and there may be numbers that cannot be reached by
your choice of a and c. Example: m % c == 0 and start at some
r with r % c == 0. Statistic experiments based on that are
obviously flawed.
Depending on where you spread or abuse your "knowledge", you
may be able to cause some damage. Even a little dose of
number theory or thought can prevent that. For a high quality
PRNG, you need more background (period, distribution of the
values, distribution modulo certain values, n-dimensional
distribution to name but a few).
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address. |  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 231,831 network members.
|