2

Im coding a project in C and Im trying to create a matrix with random numbers from 1 to 52 not repeated but keeps repeat always 1 number!

void baralhar(int b[]){
int x,y,r;
for(x=0;x<53;x++){
    r=rand() % 52+1;

    for(y=0;y<=x;y++){
        if(r==b[y])
        {
            y=0;
            r=rand() % 52+1;
        }
    }
    b[x]=r;
}

}

Output: 49 2 3 23 15 50 29 12 33 37 6 21 9 16 14 38 41 31 36 10 39 43 40 30 48 7 4 8 5 18 34 46 1 47 27 13 51 42 17 19 25 20 26 35 28 52 49 45 24 32 22 44

in this example you can see the number 49 is repeat. Can you help here?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
durooo
  • 23
  • 3
  • What you need is an array of numbers from 1 to 52, which is then shuffled. Read about shuffling algos, e.g. http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle. – PiotrK Jan 10 '14 at 23:21
  • let me guess, you're doing something related to cards? – Derek Jan 10 '14 at 23:22
  • 2
    Agree on shuffling instead of picking numbers; but you also seem to be off by 1? You appear to be trying to fill slots 0-52 with the numbers 1-52, so one of them would have to be repeated. Your array should be size 52, meaning slots 0-51. – tabstop Jan 10 '14 at 23:23
  • Crystal ball: The answer to the _next_ question is http://stackoverflow.com/questions/3067364/generating-random-numbers-in-c – chux - Reinstate Monica Jan 10 '14 at 23:25
  • http://stackoverflow.com/questions/20940694/generate-different-numbers-in-c-using-random/20940815#20940815 – BLUEPIXY Jan 10 '14 at 23:33

4 Answers4

1

You fill a 53 element array with 52 different numbers. Obviously, you're going to have a repeat.

As mentioned in comments, check how to permute 52 numbers instead, and make your array size match 52.

Edit: Also, you will always repeat the first draw (element in position 0). You reset y=0, but when you reach the end of the loop, y is counted up to 1. So position 0 is not protected from repeating.

gnometorule
  • 2,151
  • 2
  • 20
  • 29
1

You should first generate a sequence of numbers from 1 to 52 and then shuffle them. This ensures that there's no duplicates.

The following code uses Fisher-Yates shuffle algorithm.

#include <stdio.h>
#include <time.h>

int main(void)
{
    int numbers[52];
    int i, j, temp;
    srand(time(NULL));

    // putting numbers from 1 to 52 into the 'numbers' array
    for(i=0; i<52; i++)
        numbers[i] = i + 1;

    // shuffling using Fisher-Yates shuffle algorithm
    for(i=51; i>=1; i--)
    {
        // very important: pick a random number from 1 to i
        j = rand()%(i+1);

        // swapping two numbers
        temp = numbers[i];
        numbers[i] = numbers[j];
        numbers[j] = temp;
    }

    // printing the array
    for(i=0; i<52; i++)
        printf("%d, ", numbers[i]);

    return 0;
}

Here's a demo: http://ideone.com/T5AXkM, and here's a nice explanation step by step how the algorithm works: http://www.youtube.com/watch?v=tLxBwSL3lPQ.

PiotrK
  • 1,502
  • 1
  • 15
  • 33
0

Firstly, the inner cycle (for y) checks "previous" array elements from b[0] to b[x]. But element b[x] is not initialized at that point. It contains a garbage value (or whatever was in b initially). There's no point in comparing r with b[x]. Your inner cycle should stop checking at b[x - 1].

Secondly, as it has already been mentioned in the other answer, after resetting y to 0, y immediately gets incremented by the loop. So, most of the time your code ignores b[0] when checking for repeats.

Thirdly, your rand() % 52 + 1 expression can only generate 52 unique numbers, while your code is written to generate 53. Your code is supposed to loop forever. (It doesn't loop forever because of the second bug). In the title of your question you said that you need 52 numbers. Why is then your outer cycle written to make 53 iterations?

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0
void baralhar(int b[])
{

int x,y,r;

for(x=0;x<52;x++)

{
      r=rand() % 52+1;
      for(y=0;y<=x;y++)
      {
            if(r==b[y])
            {
                 y=-1;
                 r=rand() % 52+1;
            }
      }
      b[x]=r;
}

Just change in the first loop to "for(x=0;x<52;x++)

and instead of resetting y=0 make it y=-1

this will solve the second and third concern of the previous comment

then the first concern will automatically disappear..

h4ck3r
  • 1