Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. import json
  2. import random
  3.  
  4. output_name = "../input.json"
  5.  
  6. colours = [
  7.     ("red", 0.4),
  8.     ("blue", 0.4),
  9.     ("green", 0.1),
  10.     ("purple", 0.05),
  11.     ("orange", 0.05)
  12. ]
  13.  
  14. if __name__ == "__main__":
  15.     output = []
  16.  
  17.     for i in range(1000):
  18.         r = random.random()
  19.         accum = 0
  20.  
  21.         for this_colour, p in colours:
  22.             accum += p
  23.  
  24.             if r < accum:
  25.                 colour = this_colour
  26.                 break
  27.  
  28.         output.append({
  29.             "colour": colour,
  30.             "index": i
  31.         })
  32.  
  33.     with open(output_name, "w") as out_file:
  34.         json.dump(output, out_file, indent=4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement