Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- def mapRandoms(target,current,precision=1.01,testRepeat=10000):
- a, b = target, current
- countA = {i+1:0 for i in range(a)}
- countB = {i+1:0 for i in range(b)}
- def randomB():
- result = randint(1,b)
- countB[result] += 1
- return result
- if target < current:
- raise ArithmeticError("target can't be smaller then current")
- elif target <= 1 or current <= 1:
- raise ArithmeticError("random values should be more than 1")
- print("================ Mapping",target,"to",current,"================")
- i,j = 1,1
- powerA, powerB = a, b
- while True:
- j += 1
- powerA *= a
- while powerB < powerA:
- i += 1
- powerB *= b
- if powerB/powerA < precision:
- print("Coefficients to use:", i, j, "with coverage difference of only", round((powerB/powerA-1)*100,2), "% chance to repeat.")
- break
- coeffA = j
- coeffB = i
- rolls = []
- def statefulRandom():
- if target == current:
- return randomB()
- threshold = pow(a, coeffA)
- def generateRolls():
- result = threshold
- while result >= threshold:
- result = 0
- for i in range(coeffB):
- result = (result*b) + randomB()-1
- while result:
- rolls.append(result%a+1)
- result //= 7
- while len(rolls) < coeffA:
- rolls.append(1)
- if not rolls: generateRolls()
- return rolls.pop()
- for i in range(testRepeat):
- countA[statefulRandom()] += 1
- percentages = {i:round(countA[i]*100/testRepeat,2) for i in countA}
- print("Percent distribuiton of results: ", percentages)
- print(round(sum(countB.values())/testRepeat,2), "rolls on average per function.\n")
- # Test here
- mapRandoms(7,5)
- mapRandoms(10,2)
- mapRandoms(100,84)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement