0

I've been trying to understand how to print out some random numbers from my own array, dont confuse this with that i want to generate random numbers into an array, which is not what im trying to accomplish. However, my code is like this

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

int main() {
srand(time(NULL));
int myarray[] = { 2, 5, 10 };   
//Its here where i dont know how to use rand() in to make the program generate one random number from my array, either 2, 5 or 10. I've tried, but failed.

return 0;
}

Ive not found any similar question to this either, so would greatly appreciate some help.

3 Answers 3

3
int number = myarray[rand() % 3];

This generates a random number : 0 1 2 and then accesses that element from the array.

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

2 Comments

It should be considered that the result of "rand() % 3" might not be distributed evenly in [0, 2].
Excellent, my mistake was that i thought rand took a parameter, like rand(myarray). Thanks for the help!
2

You can use the following formula to generate a random number within a range:

rnd(min, max) = (rand() % (max - min)) + min;

In your case you, min = 0 and max = 3, which gives you rand() % 3.

1 Comment

It should be considered that the result of "rand() % 3" might not be distributed evenly in [0, 2].
0

The other answers use rand() % 3 for generating a (pseudo-)"random" number between 0 and 2 (including both). This might work for you, but is not really random, since the numbers returned by rand() between RAND_MIN and RAND_MAX are not distributed uniformly in terms of their divisibility through a given number n (because the equivalence classes of the modulo relation have unequal amounts of members if RAND_MAX is not a multiple of n).

A better algorithm for getting (pseudo-)random numbers in a given range is:

int RangeRandom(int min, int max) {
    int n = max - min + 1;
    int remainder = RAND_MAX % n;
    int x;
    do
    {
        x = rand();
    } while (x >= RAND_MAX - remainder);
    return min + x % n;
}

You can then use the function RangeRandom as follows:

int myArray[] = { 0, 3, 5 };
printf("%d", myArray[RangeRandom(0, 2)]);

5 Comments

Cant you just seed the rand by using srand(time(NULL)) before when you generate just 1 number?
You can seed rand() using srand, but this doesn't change the distributional issue, since the range of return values still is [RAND_MIN, RAND_MAX]
Is there some mathematical reason that rand()%3 will have distribution problem?
Yes, there is, and I tried to explain it in my answer. rand() returns numbers in the range [0, RAND_MAX]. A reasonable value for RAND_MAX would be 2147483645. Fortunately, 2147483645 is dividable through 3, so the equivalence classes which classify the numbers from 0 to 2147483645 in terms of their rest divided through 3 have the same amount of numbers in them. If we took 10 as RAND_MAX, we would have the following equivalence classes: M0 = { 0, 3, 6, 9 } M1 = {1, 4, 7, 10 } M2 = {2, 5, 8 } As you see, the numbers which give 2 as a rest after division through 3 are underrepresented.
I think your concern is that within the range of 2 billion, %3 results in ~700,000,000 occurrences of 0 and 1, while occurrences of 3 is one less. That's insignificant and nothing to worry about. RAND_MAX is usually only 0x7fff and rand() is basically obsolete and not very good anyway. rand's usage is simply forbidden in some applications such as cryptography. To make the rand function better it desperately needs entropy which has to be brought from outside. Simple additions and subtractions don't help much.

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.