Skip to content

Instantly share code, notes, and snippets.

@max6cn
Created February 5, 2016 23:06
Show Gist options
  • Save max6cn/232b5c50751e0dc6f0b1 to your computer and use it in GitHub Desktop.
Save max6cn/232b5c50751e0dc6f0b1 to your computer and use it in GitHub Desktop.
from random import randint
from operator import add
times = 10
# Step 1: Rooling dice and record result into array
genseq = map (lambda _: randint(1,6) , [i for i in range(times)] )
#Steap 2: Count occurances
# bmap : map a number into bitmap array, eg: 2 -> [0,1,0,0,0,0](one hot encoding)
bmap = lambda n : ( [ 1 if n == i+1 else 0 for i in range(6) ] )
rcnt1= reduce( lambda l1, l2 : map(add,l1,l2) , map(bmap, genseq))
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# or in one line code:
rcnt = reduce( lambda l1, l2 : map(add,l1,l2),
map ( lambda n : [ 1 if n == i+1 else 0 for i in range(6) ] ,
map (lambda _: randint(1,6) , [i for i in range(times)] )))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment