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.

8 Answers

  1. Jerome- Reply


    Well, your question is a bit vague... but if you wanted to pick a number from 0-100 but with a bias for (say) 43 and 27, you could pick a number in the range [0, 102] and map 101 to 43 and 102 to 27. It will really depend on how much bias you want to put in, what your range is etc.

  2. Jerry- Reply


    You want a mapping function between uniform density of rand() and the probability density that you desire. The mapping function can be done lots of different ways.

  3. Jesse- Reply


    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;
  4. Jim- Reply


    This is not too difficult..

    simply create an array of all possible numbers, then pad the array with extra numbers of which you want to result more often.



    Obviously when you query that array, it will call "1" the most, followed by "4"

  5. Jimmy- Reply


    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.

  6. Joe- Reply


    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();
  7. John- Reply


    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.

  8. Johnny- Reply


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

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>