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 0
s.
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.
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;
}
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!
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.