Possible Duplicate:
Biased Random Number Generator
for once I need a random number generator with at least two numbers that have higher probabality than the others.
i.e for example: random 1->10 in a sequence of 1000. Numbers A=3 and B=7.
A – should repeat approx. at least 20% of the time.
B – should repeat approx. at least 30% of the time.
That should cover at least 50% of the 1000 sequence. Also the insertion of A and B should be somewhat probable/random in themselves. Not just add A and B every Nth step.
Total/exact control isn’t needed.
Any Ideas?
I’m a noob – c++ style code would be greatly appreciated!
One way you can do this is to randomly generate a number between 0.0 and 1.0, and choose what number to generate based on that number. For example, to implement your example scenario (pseudocode):
Basically,
jis used to partition the range 1 to 10 into three parts – one part covering from 0% to 20% of the range (the firstif), the second covering from 20% to 50% of the range (i.e. 30% wide, the secondif), and the last covering the remaining 50%. Depending on which part we randomly fall in, we chose the number to generate as appropriate.