Solved

# how to choose a type given the probability of occurence of each type?

Posted on 2003-02-20
Medium Priority
241 Views
I'm writing a simulation program. Now i have a gene which can be of type1, type2 ... till type7.
I also have the probability of occurrence of each type.
How can I select a type for the gene?

0
Question by:homelesspew
• 5
• 2

LVL 6

Expert Comment

ID: 7988110
Take your probabilities and distribute them throughout a scale of 1 to 100.  Then use the random number generator modulo 100 to get a number.  Test the range of that number within an if/else struct...

For example (this is pseudo code)

int prob;

srand((unsigned;(time(NULL)); /*initial random number generator */

for ( however many you want)
{
prob = rand() % 100;
if ( prob >=0 && < 10 )
{
/* assign it to type 1, assuming type 1 has a 10% probability */
}
else if (prob >= 10 && < 30 )
{
/* assign it to type 2, assuming type 2 has a 20% probability, etc.... */
}

}
0

LVL 6

Expert Comment

ID: 7988180
If you post more details (like what the probability of each type is), I can give you specific sample code.

When you say "select a type for each gene", if type is just an integer, you could have a function that returns the number (see below).  If type is something else (a character or string), you can modify it to return whatever you need...

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

int findType()
{
/* returns 1 through 7, distributed based upon probability of each type */

int prob;

prob = (int)(rand() % 100);
if ( prob >=0 && < 10 )
{
/* assign it to type 1, assuming type 1 has a 10% probability */
return( 1 );
}
else if (prob >= 10 && < 30 )
{
/* assign it to type 2, assuming type 2 has a 20% probability, etc.... */
return( 2 );
}

}

void main()
{
int gene;

srand((unsigned;(time(NULL)); /*initial random number generator */

/* here's the function call whenever you need to assign a type (assuming type is an integer between 1 & 7 */
gene = findType();
}
0

LVL 6

Expert Comment

ID: 7988221
oops, hit submit by accident - anyhow, I was just going to continue to make 5 more 'else if' statements for types 3 through 7, with phony percentages.

If your percentages are expressed in decimal, you can easily 'scale it up' using modulo 1000, or even 10000.

For example, let's say the percentages were:

type 1 = 15.57%
type 2 = 18.26%

prob = (int)(rand() % 10000);
if (prob < 1557)
assign type 1;
else if (prob > 1557 && prob <= 3383) /* diff of 1826 */
assign type 2;
else if (prob > 3383 ... you can get it from here for the next 5...
0

Author Comment

ID: 7988745
HI,
thanks for the response..

The probabilities of occurrences are
type 1 - .194
type 2 - .004
type3 - .003
4 - .541
5 - .007
6 - .038
7  - .213

sometimes 2 or more types can have the same probability of occurences..
i have an idea . if we generate a uniform random variate(URV) from this probability distribution, we may be able to do it.But I dont know how to generate a URV from this probability distribution.Please correct me if I'm wrong.

I hope i made things clear.
Can anybody help me with some example code ??

thanks
0

LVL 85

Expert Comment

ID: 7990835
#include <stdlib.h>
#include <time.h>

float probabilities[]={0,.194,.004,.003,.541,.007,.038,.213,-1};
main(){
int i;
int type;
float sum;
srand(time(0));
for(i=0,type=0;probabilities[i]>=0;i++){
if( rand()*(sum+=probabilities[i])/RAND_MAX <= probabilities[i] ){
type=i;
}
}
printf("type%d\n",type);
}
0

LVL 6

Accepted Solution

gj62 earned 800 total points
ID: 7991044
I don't think that was really what you are looking for - it always returns the same type...

Here's code that will return types based upon their probabilities.  I converted your fractional probabilities to whole number (in thousandths) just for ease...  Obviously, there are a number of things that are hardcoded that could be parameterized:

int ps[]={194,4,3,541,7,38,213};

int findType()
{
int prob, i;

prob = (int)(rand() % 1000);
for ( i = 0; i < 7; ++i)
{
if (prob < ps[i])
return(i+1);
}
}

main()
{
int i;

srand(time(0));
for (i = 1; i < 7; ++i)
{
ps[i] += ps[i-1];
}

for (i=0;i<50;++i)
printf("Type = %d\t",findType() );
printf("done.");

}

Sample output for 50 tests of what type:

Type = 7        Type = 1        Type = 1        Type = 4        Type = 4
Type = 7        Type = 4        Type = 4        Type = 7        Type = 4
Type = 4        Type = 4        Type = 4        Type = 4        Type = 4
Type = 4        Type = 7        Type = 7        Type = 4        Type = 4
Type = 4        Type = 1        Type = 4        Type = 4        Type = 4
Type = 4        Type = 4        Type = 7        Type = 7        Type = 4
Type = 4        Type = 1        Type = 4        Type = 4        Type = 4
Type = 1        Type = 1        Type = 4        Type = 1        Type = 7
Type = 1        Type = 4        Type = 7        Type = 4        Type = 4
Type = 6        Type = 4        Type = 4        Type = 4        Type = 1

0

LVL 6

Expert Comment

ID: 7991251
So I ran it 1,000,000 times and kept track of the types that it returned - you can see that the statistics are very close to your probabilities:

type 1 = 194919    .194
type 2 = 4027      .004
type 3 = 2975      .003
type 4 = 545228    .541
type 5 = 7114      .007
type 6 = 37537     .038
type 7 = 208200    .213

Hope this helps...
0

Author Comment

ID: 8018042
thanks a lot!
that worked..
0

## Featured Post

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn hoâ€¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
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 for-loops in the C programming language.
###### Suggested Courses
Course of the Month9 days, 19 hours left to enroll