I wish to generate random-ish numbers. Each number has a different probability of being generated than others, depending on how they are rated. i.e. an array contains the probability of that number being generated, the higher the number, the more chance it has of appearing. eg:

int numberList[10] = {1,2,3,4,5,6,7,8,9,10};

int numberProbability[10] = {10,5,7,9,2,1,6,8,3,4};

1 (rated at 10) has a far higher chance of being generated than 6 (rated at 1) or 10 (rated at 4).

Any help would be greatly appreciated. Thanks!!

int numberList[10] = {1,2,3,4,5,6,7,8,9,10};

int numberProbability[10] = {10,5,7,9,2,1,6,8,3,4};

1 (rated at 10) has a far higher chance of being generated than 6 (rated at 1) or 10 (rated at 4).

Any help would be greatly appreciated. Thanks!!

neither, and what does it matter to you if it is?

obg:

I thought of doing it the second way that you described, but if I wanted to alter the probability of the numbers occuring, it would be a pain in the arse to :)

I understand the code, but I can't quite understand the method it employs.. for example, why is the initial numberProbability array altered in the first for...loop to end up like this (10 15 22 31 33 34 40 48 51 55)? then 't' is a random number between 0 and numberProbability[NBRS - 1] (which is 55)........ ahhhh! It just clicked while I was about to describe the second for loop :).. correct me if I am wrong please..

The altering of the numberProbability[] turns it into an accending array, t is then randomly generated between 0 and the highest value in the numberProbability[] (being 55). Then a for loop goes thru numberProbability[] until t is less than what is contained in numberProbability[i] effectively finding the equivalent value in numberList[] at i

I changed the line:

if (t < numberProbability[i])

to:

if (t <= numberProbability[i])

becuase if t was equal to 51, 10 would be the output, when it should be 9.

Thanks again.

FYI: EE policy is not to give explicit answers to homework questions, but more helping to understand.

Ok, to the point! If I start from the bottom, I am pretty sure that it should be if (t < numberProbability[i]) since t ranges from 0 to the total sum - 1. For example 10 is actually the 11:th possible outcome... Perhaps it's easier to think of the intervals as 1-10, 11-15, etc, and then subract 1 to all since it's 0-based.

You are right in the middle paragraph, except for the maximimum value for t, which is 54; X % 55 ranges from 0 to 54.

Then the big one... The "second way" would be something like this:

int *array, i, j, *p;

array = malloc(55 * sizeof(int)); /* Allocate enough space */

p = array;

for (i = 0; i < NBRS; i++) { /* For each element */

for (j = 0; j < numberProbability[i]; j++) /* Insert numberProbability numbers */

p[j] = numberList[i];

p += j; /* Move offset to next unset space */

} /* Your array should be done */

The reason for changing the array is that I wanted the interval limits between 0 and 55 (or 54 actually). Then all I needed to do is decide in what interval the random number ended up.

Hope that it got a little clearer. I'm going on a vacation for a week and I will not check in here during that time. ;-)

Good luck!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

#define NBRS 10

int i, t = 0;

for (i = 0; i < NBRS; i++) {

numberProbability[i] += t; /* Sorry, changing a little */

t = numberProbability[i];

}

/* Start generating */

t = rand() % numberProbability[NBRS - 1];

for (i = 0; i < NBRS - 1; i++)

if (t < numberProbability[i])

break;

t = numberList[i];

Or, you can actually make an array containing 10+5+7+9 etc elements with 10 ones, 5 twos, etc, and random up an index in that array. That is slightly faster, but demands more memory.