#### Archived

This topic is now archived and is closed to further replies.

# rand() problem

This topic is 5157 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

(from quebec, sorry about my english ) Ok, I searched on this site and found a lots of tread on rand(). But nothing similar to my problem. Here''s my prob : I generate particle trought a for loop. example : for (50 time) { new particle and rand() his direction; } Ok, so the 50 have a good random. The prob is the next frame when I create 50 new ones, they have the SAME random as the 50 old ones. This during 1 second. After one second it make a new seed. Very freak. Its like each time I enter in my for() loop it reset the seed. But after one second, it will be a different seed. This every second. So I get trails of particles. Not what I want! The first line of my main() function is : srand(time(0)); // To have different random each time I run the program. have any ideas? thx.

##### Share on other sites
sounds like a bug in your code (i.e. what you're describing is reasonable but it's not doing what you want)

how are you using the rand() function?
are you sure you're really creating new particles?

edit: question - what do you mean by "a new seed"? You shouldn't need to re-seed the random number generator. Maybe you could show us some code.

[edited by - petewood on April 13, 2004 12:18:23 PM]

##### Share on other sites
I know you said your english might not be great, but you aren''t seeding the number generator after each number are you? You only have the one srand(time(NULL)); at the beginning?

##### Share on other sites
Your problem is that you aren''t seeding your random numbers. #include <time.h> , and in the beginning of main() insert srand(time(NULL));. Your current problem is that you probably aren''t including time.h, or your seeding method is incorrect.

Hope this helps,

- CD

// Brought to you by Code_Dark
| [( Politics Forum Beta-Test )]

##### Share on other sites
srand(time(0)); or NULL, thats the same.
At the begining. Thats not the prob!
My numbers will never be the sames each time I run my prog.
Thats not the prob!
And I dont think its my code. really dont think.
Heres the code, but I dont think you will understand it. You have to understand my engine.

NbParticule = _dpeTabRTElement[i]->RunEmiter();if (NbParticule > 0)	for (j=0;j<NbParticule;j++)	{		NewPart = dpeCreateParticle(_dpeTabElement[_dpeTabRTElement[i]->getSource()]->getSpawn());		_dpeTabRTElement[NewPart]->Position = _dpeTabRTElement[i]->Position;		float SpeedRandom = _dpeTabElement[_dpeTabRTElement[i]->getSource()]->getSpeedRandom();		if (SpeedRandom > 0)		{			_dpeTabRTElement[NewPart]->Vel = _dpeTabRTElement[i]->Front * 				(_dpeTabElement[_dpeTabRTElement[i]->getSource()]->getSpeed()+					(float)(rand()%(int)(SpeedRandom*200)-(SpeedRandom*100))/100.0f);		}		else		{			_dpeTabRTElement[NewPart]->Vel = _dpeTabRTElement[i]->Front * 				_dpeTabElement[_dpeTabRTElement[i]->getSource()]->getSpeed();		}		// Bon, le spread Angle maintenant. Là faut faire la rotation d'un vecteur, ayayaye		if (_dpeTabElement[_dpeTabRTElement[i]->getSource()]->getSpreadAngle() > 0)		{			_dpeTabRTElement[NewPart]->Vel = dukPEArbitraryRotate(				_dpeTabRTElement[NewPart]->Vel,				(float)(rand()%(int)(_dpeTabElement[_dpeTabRTElement[i]->getSource()]->getSpreadAngle()*200)-(_dpeTabElement[_dpeTabRTElement[i]->getSource()]->getSpreadAngle()*100))/100.0f,				_dpeTabRTElement[i]->Up);			_dpeTabRTElement[NewPart]->Vel = dukPEArbitraryRotate(				_dpeTabRTElement[NewPart]->Vel,				(float)(rand()%36000)/100.0f,_dpeTabRTElement[i]->Front);		}	}

Thats where I use the rand()% and the freak bug is.

[edited by - daivuk on April 13, 2004 5:02:22 PM]

##### Share on other sites
So you only call srand(time(NULL)) once? And not inside any loop or anything? Because it sounds exactly like you call srand(time(NULL)) at the beginning of every frame. Since for one second, time(NULL) will equal the same value, every frame will produce the same particles, and then after a second, time(NULL) will cause srand(time(NULL)) to produce a different set of random numbers. Maybe you''re calling srand(time(NULL)) from a completely different part of your program, but are still calling it once each frame. I can''t think of any other cause that fits the symptoms as accurately as this one does.

##### Share on other sites
Oh, I''m very stupid lol
exacly that, I called srand() at the begining of my repaint loop , I put it now in my main. thank you all!

• 10
• 17
• 9
• 13
• 41