Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Probability Random Number Generator

Let's say I'm writing a simple luck game - each player presses Enter and the game assigns him a random number between 1-6. Just like a cube. At the end of the game, the player with the highest number wins.

Now, let's say I'm a cheater. I want to write the game so player #1 (which will be me) has a probability of 90% to get six, and 2% to get each of the rest numbers (1, 2, 3, 4, 5).

How can I generate a number random, and set the probability for each number?

like image 499
Alon Gubkin Avatar asked Jun 10 '10 16:06

Alon Gubkin


People also ask

How do you generate random numbers with probability?

Generate random value with probability Select a blank cell which you will place the random value at, type this formula =INDEX(A$2:A$8,COUNTIF(C$2:C$8,"<="&RAND())+1), press Enter key. And press F9 key to refresh the value as you need.

What is a random number generator in statistics?

A random number generator is a process that produces random numbers. Any random process (e.g., a flip of a coin or the toss of a die) can be used to generate random numbers. Stat Trek's Random Number Generator uses a statistical algorithm to produce random numbers.

Why is 17 the most common random number?

Seventeen is: Described at MIT as 'the least random number', according to the Jargon File. This is supposedly because in a study where respondents were asked to choose a random number from 1 to 20, 17 was the most common choice. This study has been repeated a number of times.


3 Answers

static Random random = new Random();

static int CheatToWin()
{
    if (random.NextDouble() < 0.9)
        return 6;

    return random.Next(1, 6);
}

Another customizable way to cheat:

static int IfYouAintCheatinYouAintTryin()
{
    List<Tuple<double, int>> iAlwaysWin = new List<Tuple<double, int>>();
    iAlwaysWin.Add(new Tuple<double, int>(0.02, 1));
    iAlwaysWin.Add(new Tuple<double, int>(0.04, 2));
    iAlwaysWin.Add(new Tuple<double, int>(0.06, 3));
    iAlwaysWin.Add(new Tuple<double, int>(0.08, 4));
    iAlwaysWin.Add(new Tuple<double, int>(0.10, 5));
    iAlwaysWin.Add(new Tuple<double, int>(1.00, 6));

    double realRoll = random.NextDouble(); // same random object as before
    foreach (var cheater in iAlwaysWin)
    {
        if (cheater.Item1 > realRoll)
            return cheater.Item2;
    }

    return 6;
}
like image 184
Anthony Pegram Avatar answered Oct 23 '22 22:10

Anthony Pegram


You have a few options, but one way would be to pull a number between 1 and 100, and use your weights to assign that to a dice face number.

So

1,2 = 1
3,4 = 2
5,6 = 3
7,8 = 4
9,10 = 5
11-100 = 6

this would give you the ratios you need, and would also be fairly easy to tune later.

like image 31
Matthew Vines Avatar answered Oct 23 '22 21:10

Matthew Vines


you can define array of distribution (pseudocode) :

//fair distribution

array = {0.1666, 0.1666, 0.1666, 0.1666, 0.1666, 0.1666 };

then roll the dice from 0 to 1, save to x then do

float sum = 0;
for (int i = 0; i < 6;i++)
{
   sum += array[i];
   if (sum > x) break;
}

i is the dice number.

now if you want to cheat change array to:

array = {0.1, 0.1, 0.1, 0.1, 0.1, 0.5 };

and you will have 50% to get 6 (instead of 16%)

like image 40
Andrey Avatar answered Oct 23 '22 22:10

Andrey