Advertisement
Guest User

Untitled

a guest
Jun 8th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Each time you call rand(), it will give you a number. They'll look like random numbers, but really it's just the result of a complicated mathematical formula.
  3.  
  4. The problem with this is if you run your program and it gives you 5, 421, 35, 1853 then the next time you run your program, it will give you the exact same sequence of numbers. That's annoying.
  5.  
  6. So what you do is called "seeding the random number generator". That's basically just telling it "okay, start from here". If each time you tell it to start from a different number, then the sequence will be different each time you run your program.
  7.  
  8. So...what's a number that's going to be unique each time you run your program? There are various possibilities, but the most obvious is the date+time. Obviously, next time you run your program, it will be a different time of day, or a different day, or both.
  9.  
  10. So if you use srand(time()) to seed the random number generator with the current time, you'll get a different sequence of "random" numbers each time you run your program.
  11.  
  12.     2 years ago
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement