Re: Why will this not generate a random number!?

From: Gordon Burditt (gordonb.oaoet_at_burditt.org)
Date: 10/28/04


Date: 28 Oct 2004 01:28:58 GMT


>This has been driving me crazy. I've done basic C in school, but my
>education is mainly based on object oriented design theory where Java
>is our tool. For some reason, while helping a friend with a C
>Programming lab. I cannot for the life of me generate a random number.

rand() does not generate random numbers. It generates pseudo-random
numbers. The difference may not mean much to you in your application,
but for serious users of cryptography (e.g. spies), it can mean the
difference between life and death.

>i don't know what is wrong. please help.
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <math.h>
>
>#define RAND_MAX 32768

You do not get to define RAND_MAX. The implementation does that.

>
>int main(void)
>{
> float b;
>
> b = ((float)rand()/RAND_MAX);
>
>
> printf("Random Number: %f", b);

How do you know the result you got is NOT pseudo-random?

rand() is defined to generate consistent sequences if you
are concerned with getting the same result on repeated invocations.
In that case, look up srand().

>
> system("PAUSE");
PAUSE: command not found
> return 0;
>}

                                                Gordon L. Burditt



Relevant Pages

  • Re: "Balls!!!", said Gus Webb.
    ... It's pretty indisputable that Rand was an atheist and some ... I haven't studied her life or philosophy at all myself, ... felt the Roark character was way too unbelievable. ... other very interesting designs. ...
    (rec.games.pinball)
  • The Virtue of Selfishness
    ... A very interesting article on C&L today addresses the writing of Ayn ... She took selfishness, one of the seven deadly sins, and re-invented ... his life to the welfare of others, that he doaes not sacrifice himself ... from 'The Virtue of Selfishness' --- Ayn Rand. ...
    (misc.invest.stocks)
  • Re: random lottery draw funciton
    ... from approximately 18,000 shares. ... I believe rand() uses your operating system's default randsystem call, ... the quality of the pseudo-random numbers depends on your system. ... http://www.andyhsoftware.co.uk/space:: disk and FTP usage analysis tool ...
    (comp.lang.php)
  • Re: Truly random?
    ... Computers aren't very good at generating truly ... If you need truly random numbers for cryptography, a PRNG won't ... If calling rand() or randomalone slows down ... a pseudo-random number. ...
    (comp.lang.c)
  • Re: How to generate random numbers in C
    ... Computers do not generate truly random numbers without hardware support. ... You may want pseudo-random numbers. ... process ID (but NOT in applications where real random numbers are needed, ... the return type of rand() ...
    (comp.lang.c)