| | |
Basic Unique Random Numbers
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2009
Posts: 12
Reputation:
Solved Threads: 0
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!
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!
"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).
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).
Last edited by firstPerson; Jul 31st, 2009 at 3:04 pm.
•
•
Join Date: Jul 2005
Posts: 1,699
Reputation:
Solved Threads: 273
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.
Klatu Barada Nikto
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.
Now run the shuffle.
Now you have a shuffled deck.
To deal
Technically, you need to only shufle the first five balls, but shuffle the entire deck anyway!
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.
C++ Syntax (Toggle Plain Text)
0,1,2,3 ..... 46,47,48 [0][1][2][3] .... [46][47][48]
C++ Syntax (Toggle Plain Text)
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
To deal
C++ Syntax (Toggle Plain Text)
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!
•
•
Join Date: May 2009
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jul 2005
Posts: 1,699
Reputation:
Solved Threads: 273
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.
Klatu Barada Nikto
•
•
Join Date: May 2009
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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.
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!
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Advanced C++ online classes (or local in austin, tx) for 16 yr old?
- Next Thread: Operator (+,-,*,/) program
| Thread Tools | Search this Thread |
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker linux list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg simple string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






