Hi everyone!
I just want to have random numbers which are unique.
For example I want 5 unique numbers for a lottery game I am trying to make, nothing special just a console game.
I was thinking of using loops and dead long boolean algerbra lines of || (OR) and that sort of thing.

I use srand((unsigned)time(0));
rand() % maxrand; //maxrand declared earlier.

Thanks in advance!

Recommended Answers

All 7 Replies

"I just want to have random numbers which are unique."

So whats the problem. Are you stuck somewhere, trying to implement
this? How about a trial and show us where/if you have problems.

Since its only 5 numbers, you can create an array that holds say 5 elements. Then fill the array only if the number
is unique (by checking the array).

Assuming maxrand is an int, or something that equates to an int, then the number returned by rand % maxrand should be between zero and maxrand - 1, inclusive. It will never generate maxrand per se'. If you want between 1 and maxrand then add one to the above int. Once you have the first number generated add it to a container that can be searched every time a new random number is generated and if the new random number isn't located in the container then add the new random number to the cotainer until you get the number of unique random numbers you want between 1 and maxrand, inclusive.

This definitely sounds like a homework assignment!
Key here is you said lottery game.

So sounds like you need a card shuffle algorithm for a deck of 49 cards, but you're only going to draw the first five cards.

So assuming 49 balls in the deck!
char balls[49];
Now initialize so that they're sequential.

0,1,2,3 .....      46,47,48
[0][1][2][3] ....   [46][47][48]

Now run the shuffle.

For i = 0 to 49-1 step 1        // Shuffle the deck
     nBall = i + (rand() % (49-i));
     tmp = balls[i];                 // swap index with randomized index
     balls[i] = balls[ nBall ];
     balls[ nBall ] = tmp
end for

Now you have a shuffled deck.
To deal

for i = 0 i < 5
     TheBall is balls[i]
end for

Technically, you need to only shufle the first five balls, but shuffle the entire deck anyway!

commented: Nice Explaination. I guess I learned something new. +6
commented: Nice explanation. +10

It's no homework assignment! I'm learning C++ before I go onto the National Diploma in IT which starts in a year September and programming has always intrested me.
Anyway, I know how to get random numbers, I just want them to be unique. I know that srand((unsigned)time(0)) will give me a random number, I just want them to be different. It is probably something simple, but forgive me I am a child (not literally, I'm 20) lurking on the big and exciting path of programming (at least it is too me).

Thanks again.

They you're not reading my entire post!
It explains proper use of srand()

Gives you the shuffle algorithm you indicated you needed.

Using the shuffle approach has the advantage of you being able to hardwire the possible numbers and use standard algorhithms (there is a function called random_shuffle() in the STL algorithm header) without using rand(), srand(), or loops to check for uniqueness, etc. If your implementation allows you to do so, go for it. It speeds up the process of doing what you want to do.

Using the shuffle approach has the advantage of you being able to hardwire the possible numbers and use standard algorhithms (there is a function called random_shuffle() in the STL algorithm header) without using rand(), srand(), or loops to check for uniqueness, etc. If your implementation allows you to do so, go for it. It speeds up the process of doing what you want to do.

As we say in England "Cheers Mate".
I'm only a beginner but I think I know what you mean, I have heard a few negative things about rand() but your way demonstrated above is a lot better, thanks and I will add to your rep!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.