0

I'm trying to randomize 4 different numbers in C and trying the next code:

{
    int num1 = 0, num2 = 0, num3 = 0, num4 = 0;
    int i = 0;
    while (num1 == num2 && num1 == num3 && num1 == num4 && num2 == num3 && num2 == num4 && num3 == num4 && num3 == num2)
    {
        num1 = rand() % 7;
        num2 = rand() % 7;
        num3 = rand() % 7;
        num4 = rand() % 7;
    }
    printf("%d %d %d %d\n", num1, num2, num3, num4);
}

The code suppose to check if the numbers are not equal and if they are equal, it needs to generate new numbers until they are all distinct. But for some reason, it's not working well and even right numbers it puts them as wrong and it becomes and endless loop.

What am I missing?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

4 Answers4

2

Try like this

void
get_random_values(int *values)
{
    int source[] = {0, 1, 2, 3, 4, 5, 6};
    for (int i = 0 ; i < 7 ; ++i)
    {
        int saved;
        int j = rand() % 7;
        int k = rand() % 7;
        saved = source[j];
        source[j] = source[k];
        source[k] = saved;        
    }
    values[0] = source[0];
    values[1] = source[1];
    values[2] = source[2];
    values[3] = source[3];
}

int
main(void)
{
    int values[4];
    srand(time(NULL));
    get_random_values(values);
    for (int i = 0 ; i < 4 ; ++i)
        fprintf(stderr, "%d ", values[i]);
    fprintf(stderr, "\n");

    return 0;
}

Don't forget to set the random seed srand() at the program startup or you will get the same sequence always.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Why not `int j = rand() % 7;` etc, with initialization on the same line as the declaration. It chops three lines off the source, amongst other things. – Jonathan Leffler Jan 02 '16 at 20:25
2

This code will pick 4 different numbers in the range 0 .. 6, it works by creating an array of available numbers, as each is picked it is removed from the list.

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

#define RANGE   7                   // how many different numbers
#define PICK    4                   // how many to pick

int main(void) {
    int pool[RANGE];
    int size, n, i;
    for (size=0; size<RANGE; size++) {
        pool[size] = size;          // create number pool 0..6
    }
    srand((unsigned)time(NULL));

    // pick different numbers
    for(i=0; i<PICK; i++) {
        n = rand() % size;          // random array index
        printf("%d ", pool[n]);     // select number from pool
        pool[n] = pool[--size];     // remove from pool
    }
    printf("\n");
    return 0;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
1

To get a random, unbiased sequence:

#include <stdio.h>
#include <stdlib.h>

#define N 7

int main(int argc, char **argv)
{
    // seed random number generator with first argument for easier testing 
    if (argc > 1) {
        srand(atoi(argv[1]));
    }

    int array[N] = {0,1,2,3,4,5,6};

    // Fisher–Yates shuffle:
    // https://en.wikipedia.org/w/index.php?oldid=697311634#The_modern_algorithm
    for (unsigned i = 0; i < N - 1; ++i) {
        unsigned modulo = N - i;

        // unbiased rand() % modulo:
        // https://stackoverflow.com/a/10989061/416224
        unsigned j;
        do {
            j = rand();
        } while (j >= RAND_MAX - (RAND_MAX % modulo));
        j %= modulo;

        if (j > 0) {
            int tmp = array[i];
            array[i] = array[i + j];
            array[i + j] = tmp;
        }
    }

    for (unsigned i = 0; i < N; ++i) {
        printf("%u. %d\n", i + 1, array[i]);
    }

    return 0;
}

Please follow the referenced links in the code.

Community
  • 1
  • 1
Kijewski
  • 25,517
  • 12
  • 101
  • 143
1

I would suggest universal solution:

  • array of any size

  • lower and upper bounds sent as parameters.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void random(int* values, int amount, int lower_bound, int upper_bound)
    {
        int i=0, j=0, temp;
        if(amount > upper_bound - lower_bound + 1)
            return; // if there are more numbers than given bound
    
        for(i=0; i<amount; )
        {
            temp = rand() % ( upper_bound - lower_bound + 1 ) + lower_bound;
            for(j=i-1; j>=0; --j)
                if(temp==values[j])
                    break;
    
            if(temp==values[j])
                continue;
    
            values[i]=temp;
            ++i;
        }
    }
    
    int main()
    {
        srand(time(NULL));
        int arr[4]={0,0,0,0};
        random(arr, 4, 5, 10);
        for(int i=0; i<4; ++i)
            printf("%d\n", arr[i]);
    }
    

With this you can have (for example) 10 numbers of original values from -6 to 7.

xinaiz
  • 7,744
  • 6
  • 34
  • 78