Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
183 user(s) are online (171 user(s) are browsing Forums)

Members: 0
Guests: 183

more...

Support us!

Headlines

 
  Register To Post  

Rand()
Home away from home
Home away from home


See User information
I'm using rand() to get a random number each time i want one. But on startup it gives the exact same number each time, giving me exact the same song each time i start it. Which i don't want. Why call it rand then?? Other solutions?

X5000
Go to top
Re: Rand()
Just can't stay away
Just can't stay away


See User information
@Antique

Like with any other pseudo random number generator before using rand() you need to seed it using the srand() function.

A common, portable way to seed it is to use the output of time(NULL):
srand(time(NULL));

If you seed it using the same value you will get the same sequence of numbers from it (this can have it's uses).

If you want really random numbers at cost of portability you can also use the RANDOM: device of AmigaOS (just open it as a file and read from it) or alternately you can use the entropy unit of timer.device directly as I do in my mkpasswd program (source code included).

Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2024 The XOOPS Project