Solved

Generating random-ish numbers

Posted on 2001-07-27
5
217 Views
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!!
0
Comment
Question by:Pharaoh
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:rd707
ID: 6326506
Is this homework or an assignment?
0
 
LVL 2

Accepted Solution

by:
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])
      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.
0
 

Author Comment

by:Pharaoh
ID: 6329190
rd707:
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.
0
 
LVL 2

Expert Comment

by:obg
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!
0
 
LVL 4

Expert Comment

by:rd707
ID: 6333390
Pharoah:

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

Read 'em!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now