Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def roll(N,bias):
- '''this function rolls N dimensional die with biasing provided'''
- # do something
- return result
- >> N=6
- >> bias=( 0.20,0.20,0.15,0.15,0.14,0.16,)
- >> roll(N,bias)
- 2
- import random
- sampleMassDist = (0.2, 0.1, 0.15, 0.15, 0.25, 0.15)
- # assume sum of bias is 1
- def roll(massDist):
- randRoll = random.random() # in [0,1)
- sum = 0
- result = 1
- for mass in massDist:
- sum += mass
- if randRoll < sum:
- return result
- result+=1
- print roll(sampleMassDist)
- 0.00 - 0.20 1
- 0.20 - 0.40 2
- 0.40 - 0.55 3
- 0.55 - 0.70 4
- 0.70 - 0.84 5
- 0.84 - 1.00 6
- import random
- def roll(sides, bias_list):
- assert len(bias_list) == sides
- number = random.uniform(0, sum(bias_list))
- current = 0
- for i, bias in enumerate(bias_list):
- current += bias
- if number <= current:
- return i + 1
- >>> print roll(6, (0.20, 0.20, 0.15, 0.15, 0.14, 0.16))
- 6
- >>> print roll(6, (0.20, 0.20, 0.15, 0.15, 0.14, 0.16))
- 2
- >>> print roll(6, (10, 1, 1, 1, 1, 1))
- 5
- >>> print roll(6, (10, 1, 1, 1, 1, 1))
- 1
- >>> print roll(6, (10, 1, 1, 1, 1, 1))
- 1
- >>> print roll(6, (10, 5, 5, 10, 4, 8))
- 2
- >>> print roll(6, (1,) * 6)
- 4
- abcd = dict( A=1, D=4, C=3, B=2 )
- # keys can be any immutables: 2d points, colors, atoms ...
- wrand = Walkerrandom( abcd.values(), abcd.keys() )
- wrand.random() # each call -> "A" "B" "C" or "D"
- # fast: 1 randint(), 1 uniform(), table lookup
Add Comment
Please, Sign In to add comment