Jump to content
  • Advertisement
Sign in to follow this  
Xizor3

makeing a 20 side dice

This topic is 5025 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!