Sign in to follow this  
SelethD

stupid little randomize()

Recommended Posts

SelethD    456
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

Share this post


Link to post
Share on other sites
Boku San    428
I think you mean the function rand(), found in stdlib.h (<cstdlib>).

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.

Still, I won't delete this, it might help somebody, somewhere, somehow. OOH, PANCAKES! *leaves*

Share this post


Link to post
Share on other sites
SelethD    456
randomize just seeds the random number generator, so you only call it once per run of the app.

then you use RND to get an actual random number

i just wish i could remember what function is called to get the time to seed it with

randomize(<something>);

??? help

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this