Thursday, July 28, 2016

rand7 from rand5

rand5 gives 1..5 equally.
matrix = 
[1,2,3,4,5,6,7, //7
 1,2,3,4,5,6,7, //14
 1,2,3,4,5,6,7, //21
 0,0,0,0]       //25

rand7():
    while true:
        i = (rand5()-1)*5 + rand5()-1
        if 0 <= i <= 20:
            return matrix[i]
[Hat tip to IC]

No comments:

Post a Comment