Random Number generator not Random *****

Random Number generator not Random *****

Hi:

I am using random()&01 to get a binary random
number. Turns out if I generate more than 50 numbers,
the last 30 or more are all 1s or 0s, i.e., my random
number generator is not random!

Worse, if I generate 500 numbers, 497 are 1s.
The fraction of consistent numbers increases as the number
of trials increases.

Any clues? Anybody got a pointer to an FAQ where
I can get a random number generator that is random?

Random Number generator not Random *****

> Hi:

>         I am using random()&01 to get a binary random
> number. Turns out if I generate more than 50 numbers,
> the last 30 or more are all 1s or 0s, i.e., my random
> number generator is not random!

>         Worse, if I generate 500 numbers, 497 are 1s.
> The fraction of consistent numbers increases as the number
> of trials increases.

>         Any clues? Anybody got a pointer to an FAQ where
> I can get a random number generator that is random?

Maybe you could call srand(clock()).  That'll randomize the seed, but it
doesn't really sound like that's the problem you're having.  Something to
try...

Random Number generator not Random *****

>    I am using random()&01 to get a binary random
> number. Turns out if I generate more than 50 numbers,
> the last 30 or more are all 1s or 0s, i.e., my random
> number generator is not random!

The FAQ for comp.lang.c has a section on just that.
Basically the standard random number generator is good
at generating random numbers in general, but if you extract
just the low bit the result is, as you found out, less than
random. You may try to look at a different "bit" from
the result.

--
=======================================================

=======================================================

Random Number generator not Random *****

>Hi:

>    I am using random()&01 to get a binary random
>number. Turns out if I generate more than 50 numbers,
>the last 30 or more are all 1s or 0s, i.e., my random
>number generator is not random!

[snip...]

Did you initialize the seed using srand() function? I normally
do
srand (gettimeofday());
before I actually call random(). Seems to work for me...

K.
--

I am not my *Sysadmin* I don't do emacs. I am student and I use  |oing to do w
vi. That's what I am and that's what I do - Peter Kaine (??)     |ith this \s

Random Number generator not Random *****

:>Hi:
:>
:>   I am using random()&01 to get a binary random
:>number. Turns out if I generate more than 50 numbers,
:>the last 30 or more are all 1s or 0s, i.e., my random
:>number generator is not random!
:
:[snip...]
:
:Did you initialize the seed using srand() function? I normally
:do
:  srand (gettimeofday());
:before I actually call random(). Seems to work for me...

It must only _seem_ to.  Gettimeofday(), when called correctly, always
returns 0 (at least on the UNIX I'm using).  Read the man page.

Rand() is typically a terrible random number generator, often yielding
alternating even and odd numbers continuously. Rand()&1 doesn't do too
well then.  I suggest investigating the rand48() family of functions.
Do "man -k rand48".  Also look at "Numerical Recipes in C"; it has a
whole chapter (with C source) on generating pseudo-random numbers.
(No, the code is not available for FTP, just on disk when you buy the book.)

Ron
--
A just machine to make big decisions                  | Ron Charlton

We'll be clean when their work is done                 ----------------------
We'll be eternally free yes and eternally young -- Donald Fagen, I.G.Y.

Random Number generator not Random *****

>>Hi:

>>        I am using random()&01 to get a binary random
>>number. Turns out if I generate more than 50 numbers,
>>the last 30 or more are all 1s or 0s, i.e., my random
>>number generator is not random!

>[snip...]

>Did you initialize the seed using srand() function? I normally
>do
>  srand (gettimeofday());
>before I actually call random(). Seems to work for me...

should've been

gettimeofday (tp, tzp);

Use these values to calculate the seed, i.e. make the seed for the random
generator to be a function of the current time and then do

srand (seed);

Thats's what I meant. Never post an article after 8 hours of
no-lunch-virtual-reality-session. *smile*

K.
--

I am not my *Sysadmin* I don't do emacs. I am student and I use  |oing to do w
vi. That's what I am and that's what I do - Peter Kaine (??)     |ith this \s

Random Number generator not Random *****

|> >      I am using random()&01 to get a binary random
|> > number. Turns out if I generate more than 50 numbers,
|> > the last 30 or more are all 1s or 0s, i.e., my random
|> > number generator is not random!
|>
|> The FAQ for comp.lang.c has a section on just that.
|> Basically the standard random number generator is good
|> at generating random numbers in general, but if you extract
|> just the low bit the result is, as you found out, less than
|> random. You may try to look at a different "bit" from
|> the result.

Most people replying to the original article seem to think that badri
was using rand when in fact he is using random. The man page for random
says:

The difference is
that rand(3V) produces a much  less  random  sequence  -  in
fact,  the  low  dozen  bits  generated by rand go through a
cyclic pattern.  All the  bits  generated  by  random()  are
usable.  For example,

random()&01

will produce a random binary value.

Are you saying that this statement is fertilizer?

--

Michael Salmon

#include        <standard.disclaimer>
#include        <witty.saying>
#include        <fancy.pseudo.graphics>

Ericsson Telecom AB
Stockholm

Random Number generator not Random *****

: Hi:

:       I am using random()&01 to get a binary random
: number. Turns out if I generate more than 50 numbers,
: the last 30 or more are all 1s or 0s, i.e., my random
: number generator is not random!

:       Worse, if I generate 500 numbers, 497 are 1s.
: The fraction of consistent numbers increases as the number
: of trials increases.

:       Any clues? Anybody got a pointer to an FAQ where
: I can get a random number generator that is random?

Post your code segement that makes the call.

--
--
Replies:                  |

(214) 519-3523      |
|

|> I could use some help in generating random numbers.  The problem I am having
|> is that I get the same set of numbers each time that I run the program.  I
|> have tried using srandom (seed) and entering the seed from the keyboard,
|> but this also returns the same random number.  Any help would be greatly
|> appreciated!

I have no problem at all using srandom() and random() to get random numbers,
and I expect most of the other readers of this newsgroup have no problem
either, so it's really impossible for us to diagnose the problem you are
having unless you tell us more specifically exactly what your code is doing.
For example, it would help if you would provide a short test program that
doesn't work (i.e. gets the same random numbers each time) for you.

--
Jonathan Kamens                               USnail:
MIT Project Athena                              11 Ashford Terrace

Office: 617-253-8085                          Home: 617-782-0710