Preventing number repeats in a randomized function

i am creating a shuffling system for a card game i am making for school.

i have used _card = rand() % 51;

seeing as the first card registers to 00 and the last to 51...

but i am unsure as to how to set it up to prevent a card from being selcted twice. I have confirmed it randomizes properly but i am just stuck on controlling the possibility of repeat cards
Here's something you can use to a) store your random numbers and b) prevent duplicates.
http://cplusplus.com/reference/stl/set/
(Not map. Oops).

-Albatross
Last edited on
store an array of 52 booleans to store whether each card has been drawn before? Or when a card is selected add it to a list or vector or something then when selecting the next random card check the list to see if it has already been chosen, if so choose again until the chosen card is not a repeat
ignore what I just said, maps look cool
I think the easiest way is having an array that holds all the numbers. Then you shuffle the array (with random swaps or with the shuffle function).
closed account (Lv0f92yv)
Can we see some of the code you have that your having issues with?

The rand() function is based on time, and if you use it more than once in a second it will result in the same.

1
2
3
srand( time(NULL) );
	int a = rand() % 11;
	cout << a << "\n";


Will get a random number between 0 and 10.
+1 ne555
The srand(time(NULL)); is set in a different cpp

and the rand ste up is

_card = rand() %51;
that isn't the issue i can s cout that fine

i just want to make it so it can't repeat the same one twice

also i have a slight issue with a getter for a class i made

player1.GetChipsBlk(10) - here i am trying to get "10" chips however it only reads it as one chip when i

cout<<"your current money is "<<player.GetMoney()<<" dollars!\n\n";
- rand() % 51 gives you a number between [0-50] (only 51 cards). You probably meant rand() % 52

- Make a vector of cards that has 52 entries, each entry is a unique card

- Shuffle the contents of the vector with random_shuffle: http://cplusplus.com/reference/algorithm/random_shuffle/

- instead of generating a card randomly with rand(), pop_back() it from the shuffled vector.



EDIT: as for your other problem that's impossible for us to solve without seeing code.
Last edited on
On second thought, if we're shuffling cards, having a "deck" would be a better idea. Ignore my recommendation of sets.

-Albatross
Last edited on
rand% 51 give a random set from 0 - 51 (52) entries, cause it will stop at 51, %52 will give me 53 entries.

not only does it say so in the C++ book we are using in school but on this site as well as others that reference srand use
>_> I know what I'm talking about. You must be reading the docs wrong.

x % y can never equal y.

rand() % x gives you [0-x) (x is exclusive)

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdlib>
#include <ctime>
#include <iostream>

int main()
{
  srand(time(0));

  for(int i = 0; i < 1000; ++i)
  {
    std::cout << rand() % 2;  // count how many 2's you get
  }
  std::cout << std::endl;
  std::cin.get();
}


You'll notice you only get a bunch of 0s and 1s. You never get any 2s.
closed account (Lv0f92yv)
Disch is correct. Modulo works by diving dividing the left operand by the right operand and returning (giving you) the remainder.

A remainder of n can never equal n, or else it is not the remainder.

If I have 3 people in the room and have 3 cookies for each person, I don't have any left over ( 3 % 3 = 0 ). Unless I don't like you and cut one of your cookies in half.

std::cout << rand() % 2; // count how many 2's you get


lol....
Last edited on
Topic archived. No new replies allowed.