a discrete probability distribution sampling. Create a class called Sample that supports the following two operations and has a function Object() { [native code] } that accepts an array p[] of double values as an argument. Change(i, v) changes the value of p[i] to v and random() returns an index I with a probability of p[i]/T (where T is the sum of the numbers in p[]). Use a full binary tree with implied weights of p[i] at each node. The total weight of all the nodes in a subtree should be stored in each node. Choose a random number between 0 and T to create a random index, and then use the cumulative weights to decide which branch of the subtree to explore. Change each node's weight along the path from the root when modifying p[i].
a discrete probability distribution sampling. Create a class called Sample that supports the following two operations and has a function Object() { [native code] } that accepts an array p[] of double values as an argument. Change(i, v) changes the value of p[i] to v and random() returns an index I with a probability of p[i]/T (where T is the sum of the numbers in p[]). Use a full binary tree with implied weights of p[i] at each node. The total weight of all the nodes in a subtree should be stored in each node. Choose a random number between 0 and T to create a random index, and then use the cumulative weights to decide which branch of the subtree to explore. Change each node's weight along the path from the root when modifying p[i].
Step by step
Solved in 3 steps with 2 images