Recommended Posts

I cant believe im having to ask this.
How do you use the function 'randomize()' to generate random numbers. I havent used it in soooo long i cant remember.
Im using c++
i know it has somthing to do with the time.h header
like
randomize(gettime()) or somthing like that to seed it with the time value so its 'kinda' a unique seed
please anyone know?
i searched msdn, and google, but all i get is VisualBasic crap
thanks

Link to post

Share on other sites

If you just want one random number, one time, and you don't want it to ever be different (e.g. won't use it in a loop), you should just use rand()%MAXNUMBER+1.

If you want a different value, the most common way (and best IMO) is to use srand() before you call the function. I always use it as srand((unsigned int)time(time_t*)NULL)) before I call rand(), which is why you'll want to include time.h(<ctime>). (I apologize if any of the parentheses aren't matched, I'll edit them if it's called to my attention).

Also, sorry if this is disorganized, and sorry if any of it's untrue/there's a better way, this is how I do it, and this is how I understand it.

EDIT: Hmm...maybe I assumed too much in thinking that you meant to use the rand() function. I've *seen* randomize(), but I thought it was a custom function...my bad.