This is an additional question. About random function weights

Asked 2 years ago, Updated 2 years ago, 18 views

import random
import math
import numpy as np
             #***The average distance (dis)** after n times of simulation picking t times per person


#1. Randomly generate (x.y) coordinates between 1 and 50
#2. Calculate the coordinates and distance from (0.0)
#3. Again, create (x,y) coordinates between 1 and 50
#4. Add the distance from the coordinates of 1 to 2
#5. Create coordinates and add to the previous distance
#6. Repeat t times in this way, and the total distance traveled and the average

print("How many pickers do you have?>> ")
o = int(input())
print("How many times would you like to pick per person?">> ")
t = int(input())
print("What number(n) would you like to simulate?>> ")
n = int(input())
arr = [] #Insert a distance t times per person into arr (total number of elements is n)
nx,ny = 0,0 #startcoordinates
for i in range(n):
    result = 0
    for _ in range(t):
        x = int(random.randrange(1,51)) #1 to 50 random number generation
        y = int(random.randrange(1,51)) #1 to 50 random number generation
        print("random coordinate values x=", x",y=",y) #Input to check
        distance = math.sqrt(abs(x - nx) **2 + abs(y - ny) **2)
        print("distance from previous coordinates", distance) #I put it in to check
        Cumulative distance at result += distance #result
        nx = x #Put in variable because the next distance must be the previous distance
        ny = y
    distance = math.sqrt(abs(x) **2 + abs(y) **2) #sum the distance returned from the last coordinate to the origin
    Result += distance # Cumulative distance in result
    arr.append(result)
    a = np.array([arr])
    AVG = np.mean(a) #Mean the simulation

print("Distance is "", arr, ", Mean is "", [AVG], ")
print ("Maximum distance is", [max(arr)], ", Minimum distance is", [min(arr)], and "")
In #arr, if t is 2 and n is 3, then arr contains 3 result values

Thanks to the people who answered, I finished writing it up to here.

What I'm going to do in addition is related to the random function The current coordinates are from 1 to 50, and the x and y coordinates are randomly drawn Increase the scope of the guilty vote to 0 to 90

70 percent chance of producing coordinates within 0 to 30

20 percent chance of producing coordinates within 30 to 60

10 percent chance of producing coordinates within 60 to 90

Can we give weight in this way? According to my short knowledge, I can use random.choice? I can't understand if I can add weight one by one when the coordinate value increases, so it's hard to apply it... Please give us a lot of lessons.

python

2022-09-20 14:43

1 Answers

There's no need to complicate it. Wouldn't this be a pig?

By the way, if you use a random module, doesn't the int object return? I haven't touched it recently, so it's vague.

x = int(random.randrange(1, 10))
if x < 8:
    x = int(random.randrange(1, 30))
elif x > 7 and x < 10:
    x = int(random.randrange(31, 60))
else:
    x = int(random.randrange(61, 90))


2022-09-20 14:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.