Generating random-ish numbers

Posted on 2001-07-27
Last Modified: 2010-04-15
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!!
Question by:Pharaoh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 6326506
Is this homework or an assignment?

Accepted Solution

obg earned 200 total points
ID: 6326674
Well, this does not look like any of my former homeworks...

#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])
  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.

Author Comment

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

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])
   if (t <= numberProbability[i])
becuase if t was equal to 51, 10 would be the output, when it should be 9.

Thanks again.

Expert Comment

ID: 6329568
Thanks for the score!

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!

Expert Comment

ID: 6333390

It doesn't matter to me but it is against the terms and conditions of use.

Read 'em!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question