Basic Unique Random Numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2009
Posts: 12
Reputation: ryancfc is an unknown quantity at this point 
Solved Threads: 0
ryancfc ryancfc is offline Offline
Newbie Poster

Basic Unique Random Numbers

 
0
  #1
Jul 31st, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,346
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 168
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Basic Unique Random Numbers

 
0
  #2
Jul 31st, 2009
"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).
Last edited by firstPerson; Jul 31st, 2009 at 3:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,699
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 273
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Basic Unique Random Numbers

 
0
  #3
Jul 31st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Basic Unique Random Numbers

 
2
  #4
Jul 31st, 2009
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.
  1. 0,1,2,3 ..... 46,47,48
  2. [0][1][2][3] .... [46][47][48]
Now run the shuffle.
  1. For i = 0 to 49-1 step 1 // Shuffle the deck
  2. nBall = i + (rand() % (49-i));
  3. tmp = balls[i]; // swap index with randomized index
  4. balls[i] = balls[ nBall ];
  5. balls[ nBall ] = tmp
  6. end for
Now you have a shuffled deck.
To deal
  1. for i = 0 i < 5
  2. TheBall is balls[i]
  3. end for

Technically, you need to only shufle the first five balls, but shuffle the entire deck anyway!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 12
Reputation: ryancfc is an unknown quantity at this point 
Solved Threads: 0
ryancfc ryancfc is offline Offline
Newbie Poster

Re: Basic Unique Random Numbers

 
0
  #5
Jul 31st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Basic Unique Random Numbers

 
0
  #6
Jul 31st, 2009
They you're not reading my entire post!
It explains proper use of srand()

Gives you the shuffle algorithm you indicated you needed.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,699
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 273
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Basic Unique Random Numbers

 
0
  #7
Jul 31st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 12
Reputation: ryancfc is an unknown quantity at this point 
Solved Threads: 0
ryancfc ryancfc is offline Offline
Newbie Poster

Re: Basic Unique Random Numbers

 
0
  #8
Jul 31st, 2009
Originally Posted by Lerner View Post
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC