By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

new random numbers

P: 5
hi im writing a program which generates a random # in a function several times in a row from the same function. but i have a problemit always returns the same number. is there anyway to to have it generate a different number each time its called?
Feb 18 '07 #1
Share this Question
Share on Google+
3 Replies


Ganon11
Expert 2.5K+
P: 3,652
Yes. Insert the following statement as the first line of your main() function:

Expand|Select|Wrap|Line Numbers
  1. srand((unsigned)time(0));
This will seed the random number generator based on your computer's system time, guaranteeing a set of distinct random numbers every time you run the program.
Feb 18 '07 #2

P: 5
Yes. Insert the following statement as the first line of your main() function:

Expand|Select|Wrap|Line Numbers
  1. srand((unsigned)time(0));
This will seed the random number generator based on your computer's system time, guaranteeing a set of distinct random numbers every time you run the program.
thanks im used to VB were all you have to do is use RANDOMIZE.
Feb 18 '07 #3

Ganon11
Expert 2.5K+
P: 3,652
I came from the same background. In Java, I believe the seeding is done automatically, too.
Feb 18 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.