makeing a 20 side dice

Started by
2 comments, last by Matt Apple 19 years, 7 months ago
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?
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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.
BoC HomepageLuck is a Horse to ride like any other...Luckily im not a gambler, I dont know how to ride.
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