1

I'm trying to generate 10 unique random numbers between 1 and 10. I keep getting duplicate numbers. Can someone tell me what the problem is? What am I missing or need to fix in my code? Thank you!

students[0].id = rand() % 10 + 1;
for (int i = 1; i < 10; i++)
{
    students[i].id = rand() % 10 + 1;
    for (int j = 0; j < i; j++)
    {
        if (students[i].id == students[j].id)
        {
            students[i].id = rand() % 10 + 1 ;
        }
    }
}

for (int i = 0; i < 10; i++)
{
    printf("%d\n", students[i].id);
}
4
  • Go through a sample execution in a debugger or on paper. Commented Apr 11, 2013 at 5:44
  • 1
    A random number is never guaranteed to be unique. You will have to think of a better idea :) Commented Apr 11, 2013 at 5:45
  • You should make a set of 10 numbers, and remove the next one picked (using rand) from the set, or keep calling rand until you get one that still remains in the set (then remove it). Commented Apr 11, 2013 at 5:55
  • Use this formula to generate random number M + rand() / (RAND_MAX / (N - M + 1) + 1) Commented Apr 11, 2013 at 5:59

9 Answers 9

2
if (students[i].id == students[j].id)
        {
            students[i].id = rand() % 10 + 1 ;
        }

In this line , you may get duplicate.

if students[i].id & students[j].id = 5 means it will get true. But in this line students[i].id = rand() % 10 + 1 ; ,you may get again 5

Instead of above line, you may use this code.

students[0].id = rand() % 10 + 1;
for (int i = 1; i < 10; i++)
{
    students[i].id = rand() % 10 + 1;
    for (int j = 0; j < i; j++)
    {
        if (students[i].id == students[j].id)
        {
           i--;
           break;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

The function rand() isn't guaranteed to generate unique random numbers. Moreover, your way of limiting the range (mod 10) is especially bad and is likely to generate many duplicates.

The simplest way to generate your sequence is probably to shuffle an array of 10 values from 1 to 10.

1 Comment

Agreed, though it should be made clear that the numbers in the array will not be RANDOM, they will be in a RANDOM order
1

rand() is random in nature, so there is no guarantee to give you unique results. You need to do something like this - keep track of all the numbers encountered so far and call the rand() till you find unique numbers

Comments

0

Random does not mean "No Duplicates".

Comments

0

After you entered this if clause if (students[i].id == students[j].id) and modified students[i].id, you need to check for duplicates again.

Comments

0

Try This One: replace if with while. I hope you get your answer.

students[0].id = rand() % 10 + 1;

for (int i = 1; i < 10; i++)

{

students[i].id = rand() % 10 + 1;
for (int j = 0; j < i; j++)
{
    while (students[i].id == students[j].id)
    {
        students[i].id = rand() % 10 + 1 ;
    }
}

}

for (int i = 0; i < 10; i++)

{

printf("%d\n", students[i].id);

}

Comments

0

I think you want to sample without replacement. You can store the indices in an array, randomly pick up one & simultaneously remove it from the array.

So that when you draw next time, it doesn't repeat.

Comments

0

You need recursion.

public static void main(String[] args)
{
    System.out.println(generateRandomNumbers(10, new ArrayList<Integer>()));
}

private static List<Integer> generateRandomNumbers(Integer maxLimit, List<Integer>    randomNumberList)
{
    for (int i = 0; i < maxLimit; i++)
    {
        Integer tempRandom = new Random().nextInt(10);
        if (randomNumberList.contains(tempRandom))
            generateRandomNumbers(1, randomNumberList);
        else
            randomNumberList.add(tempRandom);
    }
    return randomNumberList;
}

4 Comments

You're answering a C question with Java code, which uses recursion? What justification do you have that this unbound recursive function won't invoke undefined behaviour by proxy of a "stack overflow"?
Does it really matter? I've just wanted to show the algorithm. With C, there will be a function, and it'll call itself when generated random number exists in generated number list already. BTW, i don't guarentee the possibility of exception or error.
There are no "exceptions" in C, and C has "undefined behaviour". Code that invokes undefined behaviour may appear to function as desired on your system, while failing on other systems in subtle or devastating ways. Perhaps a segfault will be thrown if someone causes input that overflows the stack, or perhaps it'll be an opportunity for them to maliciously compromise the system...
I said i don't guarentee the possibility of exception or error. It was an example for the question. I didn't mean that i solved the question. You know C, you are the champion, solve it if you can.
0

What happens if RAND_MAX (commonly 32767) isn't evenly divisible by 10? You're likely to get values between 1 and 7 more often than 8 and 0. That's a bias.

I would suggest discarding any values greater than or equal to 32760 (or rather, RAND_MAX - RAND_MAX % 10), and using the division operator to construct your random number:

int x;
do {
    x = rand();
} while (x >= RAND_MAX - RAND_MAX % 10);
x /= RAND_MAX / 10;

You'll see a fairly drastic improvements from this; In fact, that seems like your most significant bias. However, the distribution of the values you get still isn't required to be uniform. Use a lookup table to discard any values you've previously selected:

int selected[10] = { 0 };
for (int i = 0; i < 10; i++) {
    int x;
    do {
        x = rand();
    } while (selected[x / RAND_MAX / 10] || x >= RAND_MAX - RAND_MAX % 10);
    x /= RAND_MAX / 10;
    selected[x] = 1;

    student[i].id = x;
}

for (int i = 0; i < 10; i++) {
    printf("student[%d].id: %d\n", i, students[i].id);
}

Comments

Your Answer

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.