Is there a way to simplify the finite determination method of the ratio term composed of random value generator to loop?

Asked 2 years ago, Updated 2 years ago, 13 views

It comes out after Monte Carlo integration.

Suppose you define the variables x0, x1, x2,... as random.random().

If X1/X0 < random.random(), remember the value of X1 (molecule) and start the next decision.

If X1/X0 = random.random(): The value of X0 (denominator) remains the same in the next step.

Start determining X2/X0 > random.random().

If X2/X0 > random.random(), remember the value of X2 (molecule) and start the next decision.

If X2/X0 < random.random(), maintain the value of X0 (denominator) and enter the next decision.

In other words, the numerator/denominator > random.random() is determined, and if it is larger than random, the numerator value is memorized

In the next decision, the value is re-entered and the remaining (denominator, random.random()) values are replaced with a new random number.

Repeat this way to find all N molecular values when the random value is large.

Just simply weave it into a for or an if statement, and you have to keep writing infinite if statements. ㅠ.ㅠ

I'm sure you don't understand. To explain it in a sloppy code...

import random
for x in range(100):
    X0 = random.random()
    X1 = random.random()
    X2 = random.random()

    if X1/X0 > random.random() :
        print (X1,1) # If it is greater than random, the molecular value is remembered (1 is a sign that the ratio is greater than random)
        If X2/X1 > random.random(): # The remembered X1 is located as a denominator in the next determination, and X2, random.random() is an independent random number.
            print(X2,1) # If it is larger than random, remember the value of the molecule.
            else:
                print(X1,0)# If it is less than random in the second judgment, the value of the denominator is maintained (0 is a sign that the ratio is less than random))

    else: # If the initial determination is less than random, the denominator value remains the same.
        print(X0,0)

        # It keeps repeating like this... Each time the ratio is greater than random, you need to obtain N values to remember. 
        # What the hell am I supposed to do with this?

I ask for your help...

python

2022-09-21 15:37

1 Answers

I think you should make a recursive function and spin it. I've never heard of Monte Carlo method but if I were to code it according to the given conditions...

import random

def monte_carlo(supposedly_bigger_number, the_list) :
    if len(the_list) > 9 :
        return the_list
    else :
        possibly_bigger_number = random.random()
        arbitrary_average = random.random()
        if possibly_bigger_number/supposedly_bigger_number > arbitrary_average :
            the_list.append(possibly_bigger_number)
            return monte_carlo(possibly_bigger_number, the_list)
        else :
            the_list.append(supposedly_bigger_number)
            return monte_carlo(supposedly_bigger_number, the_list)

first_number = random.random()
new_list = []
print(monte_carlo(first_number, new_list))

Read the questions again and try to get the best sense of intent and fix the demo. I hope you can use it.


2022-09-21 15:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.