Mail Archives: djgpp/2007/09/04/15:45:06
MikeC wrote:
> Folks,
>
> Here's an open invitation to make me feel foolish!
>
> Here's the program :
>
> #include <time.h>
> #include <stdlib.h>
>
> main()
> { long lng;
>
> lng = rawclock();
> printf("\nClock = %ld - ", lng);
> srand(lng);
> printf("Random numbers = %ld; %ld; %ld",random(),random(),random());
> }
>
> The idea is to seed the random number generator from the clock, so that the
> first call to random() will always produce a different result.
>
> When I run the program several times, here's what it yields :
>
> Clock = 1392294 - Random numbers = 1027100827; 143302914; 2078917053
> Clock = 1393105 - Random numbers = 1027100827; 143302914; 2078917053
> Clock = 1393314 - Random numbers = 1027100827; 143302914; 2078917053
> Clock = 1393420 - Random numbers = 1027100827; 143302914; 2078917053
> Clock = 1393471 - Random numbers = 1027100827; 143302914; 2078917053
> Clock = 1393512 - Random numbers = 1027100827; 143302914; 2078917053
> Clock = 1393548 - Random numbers = 1027100827; 143302914; 2078917053
>
> The 'random' numbers are the same every time I run the program, yet they are
> being seeded be a clock value that is different each time. I guess the wood
> is there somewhere, but the trees are getting in the way.
>
> Incidentally, I'm running Windows 2000, SP4.
>
> Advance thanks,
>
> MikeC.
What about if I want to generate numbers in the range of 1 through 50?
How do I specify my range?
Thanks,
Bill
- Raw text -