Random Number Generation

M

muttaa

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!
 
R

Richard Heathfield

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).
 
G

Gordon Burditt

May i know how to write a code that generates random numbers as many
times as one would want ?

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.
i.e. like the standard function 'rand()' or
may i get the basic logic behind it ?

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
 
R

Richard Heathfield

Gordon Burditt said:
Code does not generate random numbers. Code generates pseudo-random
numbers. For real random numbers you need hardware.

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!
 
K

Keith Thompson

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 ?

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.
 
J

Julian V. Noble

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 ?

Thanks in advance!

Google for Marsaglia.
 
E

Eric

Richard said:
muttaa said:


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!
Eric
 
E

Eric Sosman

Eric wrote On 05/19/06 20:40,:
Richard Heathfield wrote:



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!

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.
 
C

CBFalconer

Eric said:
Eric wrote On 05/19/06 20:40,:

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.

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/>
 
E

Eric

Eric said:
Eric wrote On 05/19/06 20:40,:

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.
Jesus Man, take a fucking chill pill will ya? Its not the end of the world
you know.
Eric
 
M

Michael Mair

Eric said:
Jesus Man, take a fucking chill pill will ya? Its not the end of the world
you know.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top