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

## Jerome-

2019-11-13

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.

## Jerry-

2019-11-13

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.

## Jesse-

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.

## Jim-

2019-11-13

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.

ie:

array('1',1','1','1','2','3','4','4');

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

## Jimmy-

2019-11-13

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()`

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

`exp()`

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

## Joe-

2019-11-13

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

## John-

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.

## Johnny-

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