943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 691
  • C++ RSS
Jul 31st, 2009
0

Basic Unique Random Numbers

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ryancfc is offline Offline
12 posts
since May 2009
Jul 31st, 2009
0

Re: Basic Unique Random Numbers

"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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Jul 31st, 2009
0

Re: Basic Unique Random Numbers

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jul 31st, 2009
2

Re: Basic Unique Random Numbers

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.
C++ Syntax (Toggle Plain Text)
  1. 0,1,2,3 ..... 46,47,48
  2. [0][1][2][3] .... [46][47][48]
Now run the shuffle.
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 31st, 2009
0

Re: Basic Unique Random Numbers

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ryancfc is offline Offline
12 posts
since May 2009
Jul 31st, 2009
0

Re: Basic Unique Random Numbers

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

Gives you the shuffle algorithm you indicated you needed.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 31st, 2009
0

Re: Basic Unique Random Numbers

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jul 31st, 2009
0

Re: Basic Unique Random Numbers

Click to Expand / Collapse  Quote originally posted by Lerner ...
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ryancfc is offline Offline
12 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Advanced C++ online classes (or local in austin, tx) for 16 yr old?
Next Thread in C++ Forum Timeline: Operator (+,-,*,/) program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC