1

this is my first question here, i'm making a little game, and for assigning the roles, i need random numbers. I have this code, but is possible that the numbers don't be repeated? Thanks.

void giveroles() {
 srand(time(NULL));
 int r = rand() % i + 1; 
 switch ( r ) { /* ... */ }
}
Manlio
10.9k9 gold badges55 silver badges81 bronze badges
asked Dec 23, 2012 at 12:25
2
  • In linux system you can also read /dev/randdom Commented Dec 23, 2012 at 12:29
  • @shiplu.mokadd.im Which might take a long time if your system has not enough entropy. Use /dev/urandom for faster albeit possibly not so random numbers. Commented Dec 23, 2012 at 13:38

3 Answers 3

4

If you want to randomly assign a small set of numbers, rather than generate them at random, create a list of the numbers you want, and then randomise the order of the list (iterate over the list, randomly swapping entries).

For example:

int cards[52];
for(int i = 0 ; i < 52 ; i++)
{
 cards[i] = i;
}
for(int i = 0 ; i < 1000 ; i++)
{
 int r1 = rand()%52;
 int r2 = rand()%52;
 int t = cards[r1];
 cards[r1] = cards[r2];
 cards[r2] = t;
}
for(int i = 0 ; i < 52 ; i++)
{
 printf("%d\n", cards[i]);
}

For completeness, it has been pointed out that shuffling in this fashion is biased. Here's a variation which should be unbiased:

cards[0] = 0;
for(int i = 1 ; i < 52 ; i++)
{
 int r = rand() % (i+1);
 cards[i] = cards[r];
 cards[r] = i;
}

(it should be further noted that taking the module of rand() is also likely to be biased, as the range of rand() will not be an even multiple)

answered Dec 23, 2012 at 12:40
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @JasonD, i just tried something similar and worked :P
Shuffling is a good idea, but this shuffle will lead to biased output -- not all arrangements will be equally likely. Try the Fisher-Yates shuffle which also allows one to initialize the array while shuffling.
true, and rand()%x is also biased. I suspect it doesn't matter too much here though.
3

Take out the line srand(time(NULL));. You should in general only ever do this once in your program, e.g. at the start of main(), in order to randomize the seed used by subsequent calls to rand().

answered Dec 23, 2012 at 12:26

Comments

3

If you don't want to repeat random numbers, the solution is to keep track of which random numbers you have previously used, and if you get a "hit" in your list of existing numbers, try again.

Also, using srand(time(NULL)); should only be done once. I suspect this is more the question you were actually asking.

answered Dec 23, 2012 at 12:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.