import numpy as np
import math
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())
RW = list()
For j in range(n): #n Simulation
d = 0
arr = np.random.randint(2, size=t) # +1, -1 occurrence of random number(0,1) t times
For i inarr: #Repeat +1, -1 through random numbers from above to store in position d
if i==0:
d -= 1
else:
d += 1
RW.append(d) #When simulated, save position d in the RW list and save total n simulation results
#Getting the average distance from the origin
summ = 0
For i in RW: #t Average distance (dis) after n times of moving simulations
summ += math.sqrt(RW[i]**2)
dis = summ/len(RW)
print("means distance from origin: ", dis, theoretical mean of distance: ", math.sqrt(t))
First of all, I'm thinking about it based on this code posted on the hash code.
The code I want to implement is
Sorry for the complicated explanation.
python
Except for repeating it n times, I made a very simple example.
>>> import random
>>> Previous Coordinates = 0, 0
>>> t = 50
>>> Total Distance = 0
>>> for _ in range(t):
This coordinate = random.randint(1,50), random.randint(1,50)
This is the distance traveled = ((Previous coordinates[0] - This coordinates[0])**2 + (Previous coordinates[1] - This coordinates[1])**2)**0.5
Total distance += this moving distance
Previous coordinates = this coordinates
>>> Total Distance
1282.598663019652
Of course, you can use a little bit of number-fi and make it more elegant, but basically it's like this. Copying an idle, indent for block looks a little weird, but you can look at it carefully.
You have to repeat this n times.
When you get used to it, make it simpler with Numpei.
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
For_in range(t+1) : # Add the distance back to (0, 0) from the last position (not yet clear)
distance = math.sqrt(abs(x) **2 + abs(y) **2)
arr.append(result)
a = np.array([arr])
AVG = np.mean(a) #Mean the simulation
print("distance" , arr, "and mean" , AVG, ".") #arr contains three result values if t is 2 and n is 3
I couldn't check the answer and made it up to here, but I wanted to return to the starting point from the last position, but the distance back to (0,0) was not added. Do you happen to know a solution?
© 2024 OneMinuteCode. All rights reserved.