Re: Why will this not generate a random number!?
From: Gordon Burditt (gordonb.oaoet_at_burditt.org)
Date: 10/28/04
- Next message: Arthur J. O'Dwyer: "Re: C to Java Byte Code"
- Previous message: E. Robert Tisdale: "Re: Why can't constants have commas?"
- In reply to: John Cassidy: "Why will this not generate a random number!?"
- Next in thread: Michael Mair: "Re: Why will this not generate a random number!?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Arthur J. O'Dwyer: "Re: C to Java Byte Code"
- Previous message: E. Robert Tisdale: "Re: Why can't constants have commas?"
- In reply to: John Cassidy: "Why will this not generate a random number!?"
- Next in thread: Michael Mair: "Re: Why will this not generate a random number!?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|