rand()
Re: rand()
Because the seed is the same at each call.Seraph wrote:why this function cout<<rand()%1000+300<<endl produce ever the same number?
Try calling for example
Code: Select all
srand(clock())
Are you using rand() from C runtime library or some other library?Seraph wrote:the problem is that the code is inside a do-while. if i write srand outside, it produce ever the same code, if i write srand inside the first value is ever the same, if i write srand inside and outside, the srand inside don't work.
also be sure that srand always gets another value !!!
usually you pass the current time (either day time, prog running time, or what ever time)...
if you pass the same value to srand you'll get always the same rand numbers...
usually you pass the current time (either day time, prog running time, or what ever time)...
if you pass the same value to srand you'll get always the same rand numbers...
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
i haven't add any library for rand function.Are you using rand() from C runtime library or some other library?
The code is this:
Code: Select all
srand(clock());
do{ThereIs=false;
rand1=rand()%1000+300;
rand2=rand()%1000+300;
enemy[i]->setPosition(vector3df(rand1,10,rand2));
for(int j=0;j<i;j++)
if(enemyposition==any_old_enemyposition)
ThereIs=true;
}while(ThereIs)
a-b-a-b-c-d-a-b-c-d-e-f
I would simply want a sequence of casual numbers
I guess it's because of the clock() function...
I presume it's from time.h, isn't it ???
and if you make a test like this, you'll see it always starts with the same value (with 0)...now if you make this srand(clock()) call at program start it's most likely that you always get the same seed !!!
so what I said, be sure you seed always with a different value...
I usually use GetTickCount() (under Windows)...
or use the clock() function after the user (e.g.) clicked on a button, this way it returns always undefined values...
I presume it's from time.h, isn't it ???
and if you make a test like this, you'll see it always starts with the same value (with 0)...
Code: Select all
#include <time.h>
#include <stdio.h>
int main(){
for(int r = 0; r < 100; ++r){
long t = clock();
printf("%d\n",t);
}
return 0;
}
so what I said, be sure you seed always with a different value...
I usually use GetTickCount() (under Windows)...
or use the clock() function after the user (e.g.) clicked on a button, this way it returns always undefined values...
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
GetTickCount works on all OS doesn't it?Acki wrote:I usually use GetTickCount() (under Windows)... Wink
BTW Seraph, why aren't you using GetTickCount instead of clock?
Irrlicht has clock functionality for all OS. Then it will be more cross platform (your code i.e.)
I suppose time.h is a standard library, but still, if you want cross platform as well, you should use the higher level function.
It should work properly if you seed the random number generator with GetTickCount before your loop. Doesn't that work?
I can hear birds chirping
I live in the Eye of Insanity.
I live in the Eye of Insanity.
I'm not sure, I don't use other OSs...Ulf wrote:GetTickCount works on all OS doesn't it?Acki wrote:I usually use GetTickCount() (under Windows)... Wink
but I presumed it for Windows, because I'll have to include windows.h to use it...
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
What exactly do you mean? That is a sequence of letters.Seraph wrote:this code generate ever this sequence of number
a-b-a-b-c-d-a-b-c-d-e-f
If you print out the sequence of random numbers that are generated inside the loop, what do you get?
Cause I don't know how you get a's and b's from the rand() function.
What are the s32 values returned from rand()? Are those values always the same.
I'm assuming that you do something with the rand values, and that is the sequence you are showing us.
Code: Select all
cout<<rand()%1000+300<<endl
I just looked again. We can use Timer::getRealTime() on any system. Isn't that the way.
**EDIT**
I performed this test:
Code: Select all
irr::ITimer * timer = device->getTimer();
srand(timer->getRealTime());
for (irr::u32 count = 0; count < 10; count++)
{
cout << (irr::s32)(rand()%1000+300) << endl;
}
I can hear birds chirping
I live in the Eye of Insanity.
I live in the Eye of Insanity.