Skip to main content
gamedev.net
🔒 Locked

Handling Probabilities

Started by Endemoniada Apr 7, 2005 at 9:15 PM 1 replies 800+ views
Original Post
Endemoniada
Endemoniada
Hi guys, I usually do something like this: probs[0]=0.25; probs[1]=0.5; probs[2]=1.0; ran=RanDeviate(); for(i=0;i<3;i++){ if(ran < probs) // it's that one } Well, that's fine for me but other people have a problem setting the values, they think the probs[1] will have a 50% chance of occuring. So what is a nice method ? It would be nice if I didn't have to sort anything. It would be nice if the actual values were the probabilities that would add up to 1.0 Any ideas would be appreciated.
Illco
Illco
You can do something like this (forgive me the crappy code):
#include <stdlib.h>#include <stdio.h>#define N 3void main( void ){	float probability[N];	probability[0] = 0.25f;	probability[1] = 0.50f;	probability[2] = 0.25f;	int numbers[N];	numbers[0] = 1;	numbers[1] = 2;	numbers[2] = 3;	int frequency[N];	frequency[0] = 0;	frequency[1] = 0;	frequency[2] = 0;	for ( unsigned int test = 0; test < 100; test++ )	{		int value = 0;		float random = rand() / (float)RAND_MAX;		float offset = 0.0f;		for ( unsigned int i = 0; i < N; i++ )		{			if ( random >= offset && random < probability + offset )				value = numbers;			offset += probability;		}                // MARKED LINE BELOW		frequency[value] = frequency[value] + 1;	}	for ( unsigned int i = 0; i < N; i++ )		printf( "%3d occured %4d times.\n", numbers, frequency );}


And this is interesting: if anyone knows why the marked line changes the numbers array, I'd like to know!
Endemoniada
Endemoniada
Well, this is gonna be for a banner rotation script.

So I want the user to be able to do something like this:

banner[0]=0.25;
banner[1]=0.25;
banner[2]=0.5;

So [0] and [1] each have a 25% chance of occuring and [2] has a 50% chance.

I can't think of a nice way to do it. It should be pretty simple and fast since it's going to be in PHP.

Any ideas ? Thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.