rand()

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

rand()

Post by Seraph »

why this function cout<<rand()%1000+300<<endl produce ever the same number?
tomkeus
Posts: 25
Joined: Thu Nov 12, 2009 9:50 pm

Re: rand()

Post by tomkeus »

Seraph wrote:why this function cout<<rand()%1000+300<<endl produce ever the same number?
Because the seed is the same at each call.

Try calling for example

Code: Select all

srand(clock()) 
at application start. This will make sure that each time unique sequence of random numbers is produced.
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

Post by Seraph »

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.
tomkeus
Posts: 25
Joined: Thu Nov 12, 2009 9:50 pm

Post by tomkeus »

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.
Are you using rand() from C runtime library or some other library?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

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... :lol:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

Post by Seraph »

Are you using rand() from C runtime library or some other library?
i haven't add any library for rand function.


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)
this code generate ever this sequence of number

a-b-a-b-c-d-a-b-c-d-e-f

I would simply want a sequence of casual numbers :cry:
tomkeus
Posts: 25
Joined: Thu Nov 12, 2009 9:50 pm

Post by tomkeus »

Add stdlib.h and time.h and see what happens, although I'm pretty sure Irrlicht included them somewhere for internal use.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

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)...

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;
}
now if you make this srand(clock()) call at program start it's most likely that you always get the same seed !!! :lol:
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:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

Post by Seraph »

thanks to all :D . I have only add the library time.h and works.
Ulf
Posts: 281
Joined: Mon Jun 15, 2009 8:53 am
Location: Australia

Post by Ulf »

Acki wrote:I usually use GetTickCount() (under Windows)... Wink
GetTickCount works on all OS doesn't it?

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
:twisted:

I live in the Eye of Insanity.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Ulf wrote:
Acki wrote:I usually use GetTickCount() (under Windows)... Wink
GetTickCount works on all OS doesn't it?
I'm not sure, I don't use other OSs...
but I presumed it for Windows, because I'll have to include windows.h to use it... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Ulf
Posts: 281
Joined: Mon Jun 15, 2009 8:53 am
Location: Australia

Post by Ulf »

Seraph wrote:this code generate ever this sequence of number

a-b-a-b-c-d-a-b-c-d-e-f
What exactly do you mean? That is a sequence of letters.
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
To be more clear, is that all you are doing? Printing a char representation of the number?

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;
}
Different result every time
I can hear birds chirping
:twisted:

I live in the Eye of Insanity.
Post Reply