Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

numpy random choice

from numpy import random

# weight random choices
# 10% chance of 2
# 30% chance of 4
# 60% chance of 6
# 0% chance of 8
# return array of 5 items of random choice from a list
x = random.choice([2, 4, 6, 8], p=[0.1, 0.3, 0.6, 0.0], size=(5))
print(x)
# output [4 6 4 6 4]

# 10% chance of 3
# 30% chance of 5
# 60% chance of 7
# return 2d array 3 * 5 of random chocies
x = random.choice([3, 5, 7, 9], p=[0.1, 0.3, 0.6, 0.0], size=(3, 5))
print(x)
# output
# [[7 7 7 7 7]
#  [7 3 3 5 3]
#  [7 7 7 7 7]]
Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #numpy #random #choice
ADD COMMENT
Topic
Name
4+3 =