Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. from random import randint
  2. def mapRandoms(target,current,precision=1.01,testRepeat=10000):
  3.     a, b = target, current
  4.     countA = {i+1:0 for i in range(a)}
  5.     countB = {i+1:0 for i in range(b)}
  6.     def randomB():
  7.         result = randint(1,b)
  8.         countB[result] += 1
  9.         return result
  10.  
  11.     if target < current:
  12.         raise ArithmeticError("target can't be smaller then current")
  13.     elif target <= 1 or current <= 1:
  14.         raise ArithmeticError("random values should be more than 1")
  15.  
  16.     print("================ Mapping",target,"to",current,"================")
  17.     i,j = 1,1
  18.     powerA, powerB = a, b
  19.     while True:
  20.         j += 1
  21.         powerA *= a
  22.         while powerB < powerA:
  23.             i += 1
  24.             powerB *= b
  25.  
  26.         if powerB/powerA < precision:
  27.             print("Coefficients to use:", i, j, "with coverage difference of only", round((powerB/powerA-1)*100,2), "% chance to repeat.")
  28.             break
  29.  
  30.     coeffA = j
  31.     coeffB = i
  32.     rolls = []
  33.     def statefulRandom():
  34.         if target == current:
  35.             return randomB()
  36.         threshold = pow(a, coeffA)
  37.         def generateRolls():
  38.             result = threshold
  39.             while result >= threshold:
  40.                 result = 0
  41.                 for i in range(coeffB):
  42.                     result = (result*b) + randomB()-1
  43.             while result:
  44.                 rolls.append(result%a+1)
  45.                 result //= 7
  46.             while len(rolls) < coeffA:
  47.                 rolls.append(1)
  48.         if not rolls: generateRolls()
  49.         return rolls.pop()
  50.     for i in range(testRepeat):
  51.         countA[statefulRandom()] += 1
  52.     percentages = {i:round(countA[i]*100/testRepeat,2) for i in countA}
  53.     print("Percent distribuiton of results: ", percentages)
  54.     print(round(sum(countB.values())/testRepeat,2), "rolls on average per function.\n")
  55. # Test here
  56. mapRandoms(7,5)
  57. mapRandoms(10,2)
  58. mapRandoms(100,84)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement