1

I have been working on an assignment, where I have to create a given number of arrays and fill them up with random data. The approach I would like to follow is I want the arrays to be filled with data, only a percentage. The problem is that for every array, the random values are in the same position and not spread how I would like.

I have been creating the arrays in this way:

int **array = malloc(DOC * sizeof *array);
for (i = 0; i < DOC; i++)
{
    array[i] = malloc(MAXWORDS * sizeof **array);
}

and filling them using :

srand((unsigned) time(&t));

and

for(i = 0; i < DOC; i++){
    for(j = 0; j < MAXWORDS; j++){
        array[i][rand() %percentage]=rand() %VALUE;
    }
}

Where

int percentage = rand() %MAXWORDS/10;

MAXWORDS defines the lenght of the array DOC the number of arrays VALUE is the max random value

As you can see the random values are all behaving identically. I know that this has to do with the way that srand depends on the time to generate the numbers, and the execution of the program is really fast, so the similar data are because of the "similar" time. So what I am asking is for either a different day to generate random values or some trick I could do to fill the arrays differently.

4
  • 6
    "Anyone who attempts to generate random numbers by deterministic means is, of course, living in a state of sin." -- John von Neumann Commented Apr 15, 2014 at 23:53
  • 1
    what do you mean spread how i would like? You want gaps in the array? A broader range of random integers? Commented Apr 15, 2014 at 23:53
  • 1
    No, the randomness should be enough for what you want, but the logic you have with the percentage variable doesn't really make much sense. Commented Apr 15, 2014 at 23:56
  • Well, definitely not all the numbers in the same area.Even better not connected. With gaps of zeros in between and scarce. Commented Apr 15, 2014 at 23:57

3 Answers 3

1

With "rand() % percentage" you are only picking elements within the first 10% of each array. Instead, you probably want something like this:

for (i = 0; i < DOC; ++i){
    for (j = 0; j < MAXWORDS; ++j) {
        if (rand() % 100 <= 10) {
            array[i][j] = rand() % VALUE;
        }
    }
}

This gives each elements in the array roughly a 10% chance of being initialized, which should result (for large enough arrays) in about 10% of the elements being initialized.

If you want exactly 10% of the array to be initialized, you could instead do something like placing all indices (0...j) into an array, randomizing the array, and picking the first MAXWORDS/10 indices from the randomized array for initialization.

Sign up to request clarification or add additional context in comments.

4 Comments

Well the idea was to fill only the 10 percent of the array with scarce data not all of it.
You mean only the 10% of the elements at the start of the array? Sorry, in that case I misunderstood, I thought you wanted to spread the values within the array (ie. with gaps of zero-initialized elements between the values you set).
No the 10 percent is not an obligatory rule and no NOT in the beginning.However i couldn't find a better way to scarce the data. Thanks for the idea, i will check it.
SPOT ON. Take a look (paste.debian.net/93963) EDIT, just wanted to say that this is actually a nice approach to the percentage in this situation. I marked it as solution. Cheers!
1

rand() and srand(), especially when used with %, they don't produce random numbers as uniformly distributed as you may think.

Check Mersenne twister algorithm as an alternative pseudorandom number generator.

Comments

0

I think the problem is with how you are choosing the second index: rand() % percentage will always fill towards the front of the arrays.

The standard random number generator in C (srand + rand) tends to be pretty bad at generating numbers that pass statistical tests for randomness. There are more sophisticated random number generators with better properties available as part of the GNU Scientific Library that you may find helpful.

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.