﻿ objective-c - objective c - How to make rand() more likely to select certain numbers? - oipapio - oipapio.com oipapio

# objective c - How to make rand() more likely to select certain numbers?

Is it possible to use rand() or any other pseudo-random generator to pick out random numbers, but have it be more likely that it will pick certain numbers that the user feeds it? In other words, is there a way, with rand() or something else, to pick a pseudo random number, but be able to adjust the odds of getting certain outcomes, and how do you do that if it is possible.

BTW, I'm just asking how to change the numbers that rand() outputs, not how to get the user input.

1. 2019-11-13

You can certainly use any random number generator to skew the results. Example in C#, since I don't know objective-c syntax. I assume that rand() return a number tween 0 and 1, 0 inclusive and 1 exclusive. It should be quite easy to understand the idear and convert the code to any other language.

`````` /// <summary>
/// Dice roll with a double chance of rolling a 6.
/// </summary>
int SkewedDiceRoll()
{
// Set diceRool to a value from 1 to 7.
int diceRool = Math.Floor(7 * rand()) + 1;

// Treat a value of 7 as a 6.
if (diceRoll == 7)
{
diceRoll = 6;
}

return diceRoll;
}
``````
2. 2019-11-13

In other words, is there a way, with rand() or something else, to pick a pseudo random number, but be able to adjust the odds of getting certain outcomes, and how do you do that if it is possible.

For simplicity sake, let's use the `drand48()` which returns "values uniformly distributed over the interval [0.0,1.0)".

To make the values close to one more likely to appear, apply skew function `log2()`:

``````log2( drand48() + 1.0 );  // +1 since log2() in is [0.0, 1.0) for values in [1.0, 2.0)
``````

To make the values close to zero more likely to appear, use the e.g. `exp()`:

``````(exp(drand48()) - 1.0) * (1/(M_E-1.0));  // exp(0)=1, exp(1)=e
``````

Generally you need to crate a function which would map the uniformly distributed values from the random function into values which are distributed differently, non-uniformly.

3. 2019-11-13

You can use the follwing trick This example has a 50 percent chance of producing one of your 'favourite' numbers

``````int[] highlyProbable = new int[]{...};

public int biasedRand() {
double rand = rand();
if (rand < 0.5) {
return highlyProbable[(int)(highlyProbable.length * rand())];
} else {
return (int)YOUR_RANGE * rand();
}
}
``````
4. 2019-11-13

In addition to what Kevin suggested, you could have your regular group of numbers (the wide range) chopped into a number of smaller ranges, and have the RNG pick from the ranges you find favorable. You could access these ranges in a particular order, or, you can access them in some random order (but I can assume this wouldn't be what you want.) Since you're using manually specified ranges to be accessed within the wide range of elements, you're likely to see the numbers you want pop up more than others. Of course, this is just how I'd approach it, and it may not seem all that rational.

Good luck.

5. 2019-11-13

By definition the output of a random number generator is random, which means that each number is equally likely to occur next (1/10 chance) and you should not be able to affect the outcome.

Of-course, a pseudo-random generator creates an output that will always follow the same pattern for a given input seed. So if you know the seed, then you may have some idea of the output sequence. You can, of-course, use the modulus operator to play around with the set of numbers being output from the generator (eg. %5 + 2 to generate numbers from 2 to 7).