1

I did a search but didn't really get any proper hits. Maybe I used incorrect terms?

What I want to ask about is an algorithm for simulating a biased role rather than a standard supposedly-random roll.

It wouldn't be a problem if you can't give me exact answers (maybe the explanation is lengthy?) but I would appreciate &pointers to material I can read about it.

What I have in mind is to for example, shift the bias towards the 5, 6 area so that the numbers rolls would have a higher chances of getting a 5 or a 6; that's the sort of problem I'm trying to solve.

[Update]

Upon further thought and by inspecting some of the answers, I've realized that what I want to achieve is really the Roulette Wheel Selection operator that's used in genetic algorithms since having a larger sector means increasing the odds the ball will land there. Am I correct with this line of thought?

Dr. belisarius
61.2k15 gold badges121 silver badges198 bronze badges
asked May 1, 2011 at 18:32
8
  • Do you want to simulate a biased dice physically or you want to pre-assign probabilities to each outcome? Hint: physically is difficult ... Commented May 1, 2011 at 18:36
  • Not physically, no; the probabilities are pre-assigned. I updated my question to reflect this. Commented May 1, 2011 at 18:38
  • Incidentally, I believe the relevant search terms include: "how do I sample from a multinomial distribution?" en.wikipedia.org/wiki/… Commented May 1, 2011 at 18:46
  • @dsg: If that's the case, then no wonder I got jack. Commented May 1, 2011 at 18:55
  • Are you rolling one die, or multiple dice for a total? Commented May 2, 2011 at 12:04

5 Answers 5

7

In general, if your probabilities are {p1,p2, ...,p6}, construct the following helper list:

{a1, a2, ... a5} = { p1, p1+p2, p1+p2+p3, p1+p2+p3+p4, p1+p2+p3+p4+p5} 

Now get a random number X in [0,1]

If

 X <= a1 choose 1 as outcome 
 a1 < X <= a2 choose 2 as outcome 
 a2 < X <= a3 choose 3 as outcome 
 a3 < X <= a4 choose 4 as outcome 
 a4 < X <= a5 choose 5 as outcome 
 a5 < X choose 6 as outcome 

Or, more efficient pseudocode

 if X > a5 then N=6
 elseif X > a4 then N=5
 elseif X > a3 then N=4
 elseif X > a2 then N=3
 elseif X > a1 then N=2
 else N=1

Edit

This is equivalent to the roulette wheel selection you mention in your question update as shown in this picture:

enter image description here

answered May 1, 2011 at 18:44
Sign up to request clarification or add additional context in comments.

2 Comments

Generating the helper array adds unnecessary calculations that weights easily address.
Wrote a java code based on above answer. Please find the code: censore.blogspot.in/2014/08/…
2

Let's say the die is biased towards a 3.

Instead of picking a random entry from an array 1..6 with 6 entries, pick a random entry from an array 1..6, 3, 3. (8 entries).

answered May 1, 2011 at 18:37

3 Comments

You mean pre-populating the array with the numbers according to their probability. Isn't that a bit inefficient as regards space?
There are only six entries. If it was a 1 million sided die, you would be correct. :)
It's a good solution if you don't mind being restricted within certain probability values for your dice. If you want arbitrary values you got the problem mentioned by @Andreas
2

Make a 2 dimensional array of possible values and their weights. Sum up all the weights. Randomly choose a value on the range of 0 to the sum of the weights. Now iterate through the array while keeping an accumulator of the weights seen so far. Once this value exceeds your random number, pick the value of the die represented here.

Hope this helps

answered May 1, 2011 at 18:42

Comments

1

Hmm. Say you want to have a 1/2 chance of getting a six, and a 1/10 chance of getting any other face. To simulate this, you could generate a random integer n in [1, 2, ... , 10] , and the outcome would map to six if n is in [6, 7, 8, 9, 10] and map to n otherwise.

answered May 1, 2011 at 18:37

Comments

1

One way that's usually fairly easy is to start with a random number in an expanded range, and break that range up into unequal pieces.

For example, with a perfectly even (six-sided) die, each number should come up 1/6th of the time. Let's assume you decide on round percentages -- all the other numbers should come up 16 percent of the time, but 2 should come up 17 percent of the time.

You could do that by generating numbers from 1 to 100. If the number is from 1 to 16, it comes out as a 1. If it's from 17 to 34, it comes out as a 2. If it's from 34 to 50, it comes out as a 3 (and the rest are blocks of 16 apiece).

answered May 1, 2011 at 18:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.