Solved

# Generating random-ish numbers

Posted on 2001-07-27
224 Views
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
Question by:Pharaoh
[X]
###### 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
• 2
• 2

LVL 4

Expert Comment

ID: 6326506
Is this homework or an assignment?
0

LVL 2

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

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

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

ID: 6333390
Pharoah:

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

0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Line meaning 9 96
How to install SVN Command Line Client? 5 131
A  &&  B  || (C||D) how it works in c 7 184
Assignment from incompatible pointer type? 2 75
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…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
###### Suggested Courses
Course of the Month7 days, 3 hours left to enroll