makeing a 20 side dice
in my C++ program im trying to make a 20 side dice, that diplays on the screen a random number everytime a user press 1. i have used srand(time(null)), but it gives me the same number. i am useing a for loop as well and have the srand (time(null)) inside the for loop. how do i make it give me a totaly diffrent number from 1-20 every time?
You should only call srand once at the start of the program.
As for why it gives you the same number every time, it's really simple: time is only accurate to the second (it returns the number of seconds since 1980 or something like that), and since you get your 20 numbers with in that same second, they all end up being the same.
So to reiterate, only call srand once at the start of the program. Calling rand will give you different numbers each time. srand basically resets it.
As for why it gives you the same number every time, it's really simple: time is only accurate to the second (it returns the number of seconds since 1980 or something like that), and since you get your 20 numbers with in that same second, they all end up being the same.
So to reiterate, only call srand once at the start of the program. Calling rand will give you different numbers each time. srand basically resets it.
use
srand(GetTickCount());
at the beggining of your function, or the beggining of your program. Then whenever you use
rand()% 20 + 1
you will get a number between 1 and 20.
srand(GetTickCount());
at the beggining of your function, or the beggining of your program. Then whenever you use
rand()% 20 + 1
you will get a number between 1 and 20.
Quote:Original post by Xizor3how do i make it give me a totaly diffrent number from 1-20 every time?
By "totaly different number [...] every time" do you mean that you want to randomly pick numbers between 1-20 so as they NEVER reoccur(in 20 tries)?
I wrote a lotto machine algorithm once that I could dig out if this is what you need.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement