简体   繁体   English

使用“如果”再次运行循环?

[英]Using 'If' to run the loop again?

I am producing 2 arrays of random numbers that range from -3 to 3 (this would provide to be my x and y coordinates per say). 我正在生成2个随机数数组,其范围是-3到3(这将是我说的x和y坐标)。 However I want to produce a new set of numbers if the respective x and y is greater than my radius (which is 3) by using an if loop? 但是,如果要通过使用if循环将x和y分别大于我的半径(3),我想产生一组新的数字? What do I put in after my if loop to achieve this? 我要在if循环之后输入什么来实现此目的?

from physics import *
import random
N=3

x= zeros(N,float)
y= zeros(N,float)

for i in range (0, N):
    x[i] = uniform(6)-3
    y[i] = uniform(6)-3
    if (x[n]*2+y[n]**2)**0.5 > 3:

Thanks 谢谢

Can't you just create a new range and then reset your index? 您不能只创建一个新范围然后重新设置索引吗?

from physics import *
import random
N=3

x= zeros(N,float)
y= zeros(N,float)

for i in range (0, N):
    x[i] = uniform(6)-3
    y[i] = uniform(6)-3
    if (x[n]*2+y[n]**2)**0.5 <= 3:
        x = random.sample(range(-3, 3), n)
        y = random.sample(range(-3, 3), n)
        i = 0

Most random library modules provide a routine to return a floating point number from a uniform distribution over the interval [0,1). 大多数随机库模块都提供了从间隔[0,1]上的均匀分布返回浮点数的例程。 To choose randomly from a uniform distribution over the interval [a,b), where b > a, you can just multiply by ba , and then add a . 要从区间[a,b]中的均匀分布中随机选择,其中b> a,您可以乘以ba ,然后添加a

I'm not certain which modules you're importing in your example, so I'll give examples using the standard random module, as well as the numpy.random 我不确定在示例中要导入哪个模块,因此我将使用标准随机模块以及numpy.random给出示例

random 随机

import random
def rand_range(a, b):
   return a + random.uniform() * (b - a)

x = [rand_range(-3, 3) for i in range(3)]
y = [rand_range(-3, 3) for i in range(3)]

numpy.random numpy.random

Numpy allows one to vectorize operations for speed and clarity. Numpy允许对向量进行矢量化处理,以提高速度和清晰度。

import numpy as np
def rand_range(a, b, size):
    return a + np.random.random(size) * (b - a)

x = rand_range(-3, 3, (3))
y = rand_range(-3, 3, (3))

or, create an 2D array of 2 rows of 3 elements, and use unpacking 或者,创建一个由2行3个元素组成的2D数组,然后使用拆包

x, y = rand_range(-3, 3, (2, 3))

bounds checking 边界检查

In order to ensure the points generated meet the criteria of being with 3 units of x, y = (0, 0), you can do something like the following: 为了确保生成的点满足以x,y =(0,0)为3的单位存在的条件,可以执行以下操作:

x = []
y = []
for i in range(3):
   xi, yi = 3, 3    #initally set candidates to fail check
   while xi ** 2 + yi ** 2 > 9:
      xi = rand_range(-3, 3)
      yi = rand_range(-3, 3)
   x.append(xi)
   y.append(yi)

Given the underlying random method returns a value on the interval [0,1), these arrays should all pass the following assertion: 给定基础随机方法在间隔[0,1)上返回一个值,这些数组都应通过以下声明:

for xi, yi in zip(x,y):
   assert xi**2 + yi**2 <= 9

@mtadd @mtadd

I am answering my question because I found a way to solve it. 我正在回答我的问题,因为我找到了解决问题的方法。 However, I'm curious whether the way you solved it is more efficient than my process mtadd. 但是,我很好奇您解决问题的方法是否比我的流程mtadd更有效。

In my physics import is where the random and numpy module are stored, hence why it isn't used in my code. 在我的物理导入中,是random和numpy模块的存储位置,因此为什么我的代码中没有使用它。

from physics import *

N=3 #Number of point charges

x= zeros(N,float) #grid
y= zeros(N,float)
i=0
for i in range (0, N): #Loop to ensure two values are <= 3
    while i < N:
        x[i] = uniform(6)-3
        y[i] = uniform(6)-3
        if x[i] ** 2 + y[i] ** 2 <= 9:
            i+=1
        else:
            i=0

print x,y

Thanks for the help guys 谢谢你们的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM