0

**This is not generating a number, it is rearranging the number in 1d array Need help in randomizing the sequence of 0,1,2,3 number in rbox array. however when random, it shows 1,1,1,1 or 2,2,2,2 etc. anyone have idea how to randomize the sequence into 0,3,2,1 or 2,0,1,3 etc?

srand(time(NULL));
int randomm = rand()%4;
int rbox[4];
int x;

for(x=0;x<4;x++)
{
    rbox[x] = 0;
}
for(x=0;x<4;x++)
{
    rbox[x] = randomm;
}
for(x=0;x<4;x++)
{
    printf("%i", rbox[x]);
}
sim tong
  • 13
  • 3
  • Take a look at [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle). – dbush Nov 16 '17 at 16:44
  • 1
    You are generating _one single_ random number and then you put that number in the 4 elements of the array `rbox`. So what dou you expect? I suspect you make some very wrong assumptions on programming. Start reading a good beginner's book. – Jabberwocky Nov 16 '17 at 16:46

3 Answers3

0
for(x=0;x<4;x++)
{
    rbox[x] = rand()%4;
}

Assign each element of array to a different output of rand().

Here you are assigning every element of array rbox to same value (random) - that explains why you are getting same value.


Shuffle of the array:

As from user's comment it seems that OP wants to create shuffle of the array.

Now to do that either you can initialize the whole array with values 0,1,2,3 and then apply this algorithm

-- To shuffle an array a of n elements (indices 0..n-1):
for i from n−1 downto 1 do
     j ← random integer such that 0 ≤ j ≤ i
     exchange a[j] and a[i]

This is known as Fisher Yates Shuffle.


Also other ways...

Another way would be to follow this algorithm :-

  1. Keep track of the range of numbers (this is basically highestNumInRange - iteration Number).
  2. And then keep track of the number of elements you need to generate.

3.1. If the ratio of 2 multiplied with rand() is less than 1 then put the number of iteration in the array.

3.2. Else increase the iteration.

Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
0

This is prime example of bad coding.

randomm variable gets the value once and you are assigning the same value four times to your array in the loop.

Do this,

for(x=0;x<4;x++)
{
    rbox[x] = rand()%4;
}

But this will have a chance of repeating random values. You will need to verify that the value has previously occurred or not in the array on every iteration.

0

Fill the array, then shuffle it and print the shuffled array.

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

#define SIZE 4

int main() {
    int rbox[SIZE] = { 0};
    int loop = 0;
    int index = 0;
    int swap = 0;

    srand ( time( NULL));
    for ( loop = 0; loop < SIZE; loop++) {
        rbox[loop] = loop;
    }
    loop = SIZE;
    while ( loop) {
        index = rand ( ) % loop;
        loop--;
        swap = rbox[index];
        rbox[index] = rbox[loop];
        rbox[loop] = swap;;
    }
    for ( loop = 0; loop < SIZE; loop++) {
        printf ( "%d ", rbox[loop]);
    }
    printf ( "\n");

    return 0;
}
user3121023
  • 8,181
  • 5
  • 18
  • 16