0

I was trying to generate a random array of integers of length 16 inside a loop. The elements of the array will lie inside [0, 255]. I thought assigning random integers from [0, 255] would suffice; but it does not work (one random array is created which remains unchanged over iterations). Then I tried shuffling, but the situation does not improve (I also notice that the probability of 0 in the 'random' array is significantly larger).

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#define Shuffle

/* Generate a random integer array of length `size` with elements from [0, 255] */
int* random_sample(int size) 
{
int i, j, k;
int *elements = malloc(size*sizeof(int));

/* Assign random integers */
for (i = size - 1; i > 0; --i)
{   
    elements[i] = rand() % 256;
}

/* Shuffle */
#ifdef Shuffle
for (i = size - 1; i > 0; --i) {
    j = rand() % size;

    k = elements[i];
    elements[i] = elements[j];
    elements[j] = k;
}
#endif

return elements;
}

int main(int argc, char const *argv[])
{
    int LENGTH = 16, i, iteration;
    int *random_array = random_sample(LENGTH);

    srand(time(NULL));

    for (iteration = 0; iteration < 10; ++iteration)
    {
        for (i = 0; i < LENGTH; ++i) 
            printf("%d, ", random_array[i]); 

        puts("");
    }

    return 0;
}

A typical output looks like:

0, 227, 251, 242, 171, 186, 205, 41, 236, 74, 255, 81, 115, 105, 198, 103, 
0, 227, 251, 242, 171, 186, 205, 41, 236, 74, 255, 81, 115, 105, 198, 103, 
0, 227, 251, 242, 171, 186, 205, 41, 236, 74, 255, 81, 115, 105, 198, 103, 
0, 227, 251, 242, 171, 186, 205, 41, 236, 74, 255, 81, 115, 105, 198, 103, 
0, 227, 251, 242, 171, 186, 205, 41, 236, 74, 255, 81, 115, 105, 198, 103, 
0, 227, 251, 242, 171, 186, 205, 41, 236, 74, 255, 81, 115, 105, 198, 103, 
0, 227, 251, 242, 171, 186, 205, 41, 236, 74, 255, 81, 115, 105, 198, 103, 
0, 227, 251, 242, 171, 186, 205, 41, 236, 74, 255, 81, 115, 105, 198, 103, 
0, 227, 251, 242, 171, 186, 205, 41, 236, 74, 255, 81, 115, 105, 198, 103, 
0, 227, 251, 242, 171, 186, 205, 41, 236, 74, 255, 81, 115, 105, 198, 103, 

I tried several variations of the above code, but none of them works. Need help!

EDIT

I was trying variations of the code, and mistakenly printed the same unchanged array (as pointed out by some others - thanks to them); originally I wrote

int main(int argc, char const *argv[])
{
    int LENGTH = 16, i, iteration;
    int *random_array;

    srand(time(NULL));

    for (iteration = 0; iteration < 10; ++iteration)
    {
        random_array = random_sample(LENGTH);
        for (i = 0; i < LENGTH; ++i) 
            printf("%d, ", random_array[i]); 

        puts("");
    }

    return 0;
}

which print much more 0s.

EDIT (2)

Thanks to @pmg, I found the problem. Inside random_sample function, changing the first
for (i = size - 1; i > 0; --i) to for (i = size - 1; i >= 0; --i) works fine!

Thank you all.

4
  • 2
    you print the same unmodified array Commented Apr 17, 2014 at 15:45
  • 1
    You also seed AFTER generating the random numbers. Too late. Commented Apr 17, 2014 at 15:46
  • 1
    "(one random array is created which remains unchanged over iterations)" - that's exactly what you code is supposed to do. why did you expect something else? Commented Apr 17, 2014 at 15:47
  • 2
    You call random_sample() once. Inside the function you never assign to elements[0]. Use the standard for loop construct: for (i = 0; i < LENGTH, i++) /* ... */;; you don't need to deviate from the norm. Commented Apr 17, 2014 at 15:47

3 Answers 3

3

Try the code below. For the array to contain different (random) values at every iteration, you need to put different values in it :)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#define Shuffle

/* Generate a random integer array of length `size` with elements from [0, 255] */
int* random_sample(int size) 
{
        int i, j, k;
        int *elements = malloc(size*sizeof(int));

        /* Assign random integers */
        for (i = size - 1; i > 0; --i)
        {   
                elements[i] = rand() % 256;
        }

        /* Shuffle */
#ifdef Shuffle
        for (i = size - 1; i > 0; --i) {
                j = rand() % size;

                k = elements[i];
                elements[i] = elements[j];
                elements[j] = k;
        }
#endif

        return elements;
}

int main(int argc, char const *argv[])
{
        int LENGTH = 16, i, iteration;
        int *random_array;

        srand(time(NULL));

        for (iteration = 0; iteration < 10; ++iteration)
        {
                random_array = random_sample(LENGTH);
                for (i = 0; i < LENGTH; ++i) 
                        printf("%d, ", random_array[i]); 

                puts("");
                free(random_array);
        }

        return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Inside the function you never assign to elements[0]. Use the standard for loop construct: for (i = 0; i < LENGTH, i++) /* ... */;; you don't need to deviate from the norm.
I just fixed the relevant portion of OPs code but a good catch none the less.
2

Here's some comments on your code to clarify the problem.

int main(int argc, char const *argv[])
{
    int LENGTH = 16, i, iteration;
    int *random_array = random_sample(LENGTH);   // ARRAY is created and filled in here.

    srand(time(NULL));  // Random generator is initialized here.
                        // WAIT WHAT? You already filled in the array above!
                        // It is too late now to initialize the Generator!

1 Comment

+1 but if the PRNG is not seeded, it works normally as if seeded with srand(1);. This is not his immediate issue.
1

In addition to what others have said above, I think its important to note the following on the rand function as written in the man page:

Note: The versions of rand() and srand() in the Linux C Library use the same random number generator as random(3) and srandom(3), so the lower-order bits should be as random as the higher-order bits. However, on older rand() implementations, and on current implementations on different systems, the lower-order bits are much less random than the higher-order bits. Do not use this function in applications intended to be portable when good randomness is needed. (Use random(3) instead.)

There are two possible alternatives:

1) Use the random function as recommended by the note.

2) Use the higher-order bits of the random number as follows:

elements[i] = (rand() * 256) / RANDMAX;

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.