![]() |
srand() aberration ???
well well, I feel kinda n00b here but something is puzzling me.
If I do Code:
#include <stdio.h> Now if I do Code:
#include <stdio.h> But could someone tell me WHY on Earth I need to call rand() a great number of times before using it really to have a random number ? Or put it differently why is it ALWAYS the same numbers that pop up during the first rand() calls, and this NO MATTER what I put in the seed ? Any explanation ???:( |
Re: srand() aberration ???
I think you have something messed up somewhere. This code generates the following output on my machine...
Code:
#include <stdlib.h> Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 Copyright (C) Microsoft Corp 1984-1998. All rights reserved. x.cpp Microsoft (R) Incremental Linker Version 6.00.8447 Copyright (C) Microsoft Corp 1992-1998. All rights reserved. /out:x.exe x.obj C:\TEMP>x rand = 15 rand = 90 rand = 54 rand = 10 rand = 29 rand = 18 rand = 29 rand = 70 rand = 83 rand = 56 C:\TEMP>x rand = 18 rand = 38 rand = 18 rand = 73 rand = 44 rand = 45 rand = 42 rand = 43 rand = 94 rand = 8 C:\TEMP>x rand = 21 rand = 19 rand = 14 rand = 69 rand = 90 rand = 72 rand = 54 rand = 16 rand = 38 rand = 60 botman |
Re: srand() aberration ???
heh weird stuff I tried this:
Code:
#include <stdio.h> |
Re: srand() aberration ???
that's what I have here too. The first numbers are ALWAYS the same.
But now, even more weird: I notice that instead of doing Code:
srand (time (NULL)); // initialize random seed Code:
srand (GetTickCount ()); // initialize random seed What the ... ???:( @botman I might be wrong, but I think doing Code:
rand () % 100 |
Re: srand() aberration ???
yep, doing a modulo isnt good since there are then numbers ( 65537 e.g. when the biggest is 65538 ) which are not possible, therefore not all values inside 0-100 will have the same probability. Although it's not a big deal when having RAND_MAX somewhere at 2^16 and the modulo @100
|
Re: srand() aberration ???
Tried this, to display rand() before the float roundup:
Code:
#include <stdio.h> 1) On successive run rand() seems to give *very close values*, so this explains why when you do "(int) (100 * (float) r / (RAND_MAX + 1)))" it gives the same value because the "r" values close to each other (this is a bad!). That explains why botman test does not show this behavior (no roundup). 2) Also time() give time in seconds, so if you run the test in the same second you'll have the same values because the seed is the same. That explains why GetTickCount() works better. So it seems to make sure rand() doesn't start with close numbers you have to make sure the seeds are not close to each other, so GetTickCount() is a better candidate than time(). Bad rand. 9_9 |
Re: srand() aberration ???
well, but time() is seconds since 1900 or similar, so starting in the same second of a minute should make much difference ...
another possibility would be initializing with the lower 32 bits of the 64 bit counter in newer x86 cpu's running at cpu speed |
Re: srand() aberration ???
rand() is generally considered as a poor random number generator.
You can use this instead, and it generates a 32 bit result instead of 16 bit Code:
unsigned long idnum; |
Re: srand() aberration ???
Why are you calling rand within srand()? Isn't srand supposed to be a seeded RNG? Why do you have it dependant on rand()?
|
Re: srand() aberration ???
Because you HAVE to call srand() at least once if you intend to use rand()... else each time you run the program you'll have the same series of numbers.
rand() is based on a math suite. Not sure its the real word. It's just like the math quizs for children : given the number you have, and the number before, and the number before it, deduce the number that's going to come right after. rand() is just that. If you don't initialize the seed (that is, the initial number of the suite) the output values will always be the same. hrm, I should go to bed. sorry. 8am here. |
All times are GMT +2. The time now is 03:12. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.