Sign in to follow this  
Xizor3

makeing a 20 side dice

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this