Velocity Reviews > Random-numbers...?

# Random-numbers...?

Koppe74
Guest
Posts: n/a

 09-21-2007
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

Mark Bluemel
Guest
Posts: n/a

 09-21-2007
Koppe74 wrote:
> Sorry for this newbee question, but here goes...

That's OK, but you should look at the FAQ - http://c-faq.com - before

> 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 ?

That's FAQ 13.6 - http://c-faq.com/lib/randrange.html

LPoD
Guest
Posts: n/a

 09-21-2007
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.

Keith Thompson
Guest
Posts: n/a

 09-21-2007
LPoD <(E-Mail Removed)> writes:
> 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

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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Guest
Posts: n/a

 09-21-2007
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.

--
Tor <torust [at] online [dot] no>

Guest
Posts: n/a

 09-23-2007

[...]

> 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

--
Tor <torust [at] online [dot] no>

Guest
Posts: n/a

 09-24-2007

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

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.

--

Guest
Posts: n/a

 09-24-2007

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

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

--
Tor <torust [at] online [dot] no>