1 Replies - 1782 Views - Last Post: 12 May 2012 - 12:26 PM

#1 tbob13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-May 12

rand() repeat numbers

Posted 12 May 2012 - 12:17 PM

I am a beginner programmer, and one of my apps has the rand() function as a fundamental part of it. However, whenever I run the app, it generates the same numbers in the same order. I can't figure out what is wrong with it, are there any other random methods or bug fixes that you can suggest?

Attached File(s)


Is This A Good Question/Topic? 0
  • +

Replies To: rand() repeat numbers

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: rand() repeat numbers

Posted 12 May 2012 - 12:26 PM

You have to use the srand function to seed the random number generator before you call rand() for the first time. As long as you use a different seed each time you run the program (commonly the current system time is used), you'll get a different sequence of random numbers on each run.

If you don't call srand (or call it with the same argument on each run), the random generator will use the same seed each time, which will cause it to produce the same sequence of random numbers on each run.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1