Random-numbers...?

K

Koppe74

Sorry for this newbee question, but here goes...

I of course knows about the rand()-function, but are there
any functions that yields an integer randomnumber
between 0 (or 1) and a *programmer defined* number - e.g.
between 0 and 47 ? I tried redefining RAND_MAX but
that obviously wasn't the way to do it...

Alternatively a function returning a random float between
0 and 1 could be used...

As a last ditch, do anybody have a function that uses
rand() and RAND_MAX to "translate" rand()'s random-
numbers to any of the above... maybe also improving
the "randomness" ? I need to make random-numbers
with a value between 1 and 30-to-50, so it obviously
need to fairly random in those ranges...

-Koppe
 
L

LPoD

Koppe74 pisze:
Sorry for this newbee question, but here goes...

I of course knows about the rand()-function, but are there
any functions that yields an integer randomnumber
between 0 (or 1) and a *programmer defined* number - e.g.
between 0 and 47 ? I tried redefining RAND_MAX but
that obviously wasn't the way to do it...


RTFFAQ:

http://c-faq.com/lib/randrange.html


ch.
 
K

Keith Thompson

LPoD said:

RTFFAQ yourself (the first 'F' stands for "Fine", of course):

[Note to web authors, catalogers, and bookmarkers: the URL
<http://www.c-faq.com/> is the right way to link to these
pages. All other URL's implementing this collection are subject to
change.]

When I cite the FAQ, I always post the base URL of the FAQ and the
relevant question number. This avoids problems if the URLs are
changed. It also encourages (well, forces) the reader to start at the
front page and navigate to the relevant question, and perhaps learn
something on the way.
 
T

Tor Rustad

Koppe74 wrote:

[...]
As a last ditch, do anybody have a function that uses
rand() and RAND_MAX to "translate" rand()'s random-
numbers to any of the above... maybe also improving
the "randomness" ?

Well, rand() isn't a RNG, given the same seed, it will be very much
deterministic. :)

Improving a bad PRNG, by some private tricks, is usually a bad idea, you
are better off by using another PRNG.

True RNG (TRNG) are more interesting and are typically biased. A method
to unbias TRNG, lets consider a bit stream:

if pair of bits are equal, consider next pair
if pair of bits are different, emit previous pair.
 
T

Tor Rustad

Tor Rustad wrote:

[...]
True RNG (TRNG) are more interesting and are typically biased. A method
to unbias TRNG, lets consider a bit stream:

if pair of bits are equal, consider next pair
if pair of bits are different, emit previous pair.

Ooops... change last line to:

if pair of bits are different, emit 1 or 0
 
T

Thad Smith

Tor said:
Tor Rustad wrote:

Ooops... change last line to:

if pair of bits are different, emit 1 or 0

By "emit 1 or 0" do you mean "emit 1 or 0, randomly chosen"? ;-)

It would work to emit the second bit of the pair.
 
T

Tor Rustad

Thad said:
By "emit 1 or 0" do you mean "emit 1 or 0, randomly chosen"? ;-)
LOL

It would work to emit the second bit of the pair.

Yes. Alternatively, emit the first bit of the pair. The technique is due
to von Neumann, since lots of bits will be discarded, more efficient
methods exists.

In practice, I think most implementations rather use a hashing scheme,
even if it isn't a provable method for un-skrewing a bit-sequence. Also,
there are methods based on Fast Fourier Transforms.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top