简体   繁体   English

Python中的随机生成器函数

[英]Random generator function in Python

I have been learning Java since September, but have been given an assignment for a science course by a professor to create a program in Python that will generate a random diameter using a max and minimum value, and 15 random points within this sphere (x, y, z). 我从九月开始学习Java,但是教授已经为一门科学课程分配了一个用Python创建程序的任务,该程序将使用最大值和最小值生成随机直径,并在此范围内生成15个随机点(x, y,z)。

I need to make a random number generator that generates a number between 0.0 and 1.0 so I can plug it into my formula to find a random diameter. 我需要创建一个随机数生成器,生成一个介于0.0和1.0之间的数字,这样我就可以将它插入我的公式中以找到随机直径。 If the random number is RN, it would be: [(RN*(max-min))+min] 如果随机数是RN,则为:[(RN *(max-min))+ min]

At first I used this random function: 起初我使用了这个随机函数:

from random import*
RN=random():

The problem is, this random function is [0.0, 1.0). 问题是,这个随机函数是[0.0,1.0)。 In other words, it does not include 1.0. 换句话说,它不包括1.0。 How can I create a function that includes 1.0? 如何创建包含1.0的函数?

Also, if you don't mind, can you help me with finding the y and z coordinates? 另外,如果你不介意,你能帮我找到y和z坐标吗? I know how to find x. 我知道如何找到x。

The formula for the y value is y=+ or - sqrt(r^2-x^2) (to be randomly generated too). y值的公式是y = +或 - sqrt(r ^ 2-x ^ 2)(也是随机生成的)。 I would have the x value, which is the result from the random function [0.0-1.0], and the radius which would be half my diameter. 我会得到x值,这是随机函数[0.0-1.0]的结果,半径是我直径的一半。 I am a complete beginner at Python, how do I initialize my x and y and put the above formula in? 我是Python的初学者,如何初始化我的x和y并将上述公式放入?

The formula for z is similar, z=+ or - sqrt(-x^2-y^2+r^2) (to be randomly generated as well) z的公式类似,z = +或 - sqrt(-x ^ 2-y ^ 2 + r ^ 2)(也是随机生成的)

These are using the formulas for a circle: radius: r=sqrt(x^2+y^2) sphere:r=sqrt(x^2+y^2+z^2) 这些是使用圆的公式:radius:r = sqrt(x ^ 2 + y ^ 2)sphere:r = sqrt(x ^ 2 + y ^ 2 + z ^ 2)

I would be incredibly grateful if you could answer any part of my question, thank you so much for taking the time to read this!! 如果你能回答我问题的任何部分,我将非常感激,非常感谢您花时间阅读这篇文章! **by the way, I am usinHi! **顺便说一下,我是usinHi! I have been learning Java since September, but have been given an assignment for a science course by a professor to create a program in Python that will generate a random diameter using a max and minimum value, and 15 random points within this sphere (x, y, z). 我从九月开始学习Java,但是教授已经为一门科学课程分配了一个用Python创建程序的任务,该程序将使用最大值和最小值生成随机直径,并在此范围内生成15个随机点(x, y,z)。

I need to make a random number generator that generates a number between 0.0 and 1.0 so I can plug it into my formula to find a random diameter. 我需要创建一个随机数生成器,生成一个介于0.0和1.0之间的数字,这样我就可以将它插入我的公式中以找到随机直径。 If the random number is RN, it would be: [(RN*(max-min))+min] 如果随机数是RN,则为:[(RN *(max-min))+ min]

At first I used this random function: 起初我使用了这个随机函数:

from random import* RN=random(): 来自随机导入* RN = random():

The problem is, this random function is [0.0, 1.0). 问题是,这个随机函数是[0.0,1.0)。 In other words, it does not include 1.0. 换句话说,它不包括1.0。 How can I create a function that includes 1.0? 如何创建包含1.0的函数?

Also, if you don't mind, can you help me with finding the y and z coordinates? 另外,如果你不介意,你能帮我找到y和z坐标吗? I know how to find x. 我知道如何找到x。

The formula for the y value is y=+ or - sqrt(r^2-x^2) (to be randomly generated too). y值的公式是y = +或 - sqrt(r ^ 2-x ^ 2)(也是随机生成的)。 I would have the x value, which is the result from the random function [0.0-1.0], and the radius which would be half my diameter. 我会得到x值,这是随机函数[0.0-1.0]的结果,半径是我直径的一半。 I am a complete beginner at Python, how do I initialize my x and y and put the above formula in? 我是Python的初学者,如何初始化我的x和y并将上述公式放入?

The formula for z is similar, z=+ or - sqrt(-x^2-y^2+r^2) (to be randomly generated as well) z的公式类似,z = +或 - sqrt(-x ^ 2-y ^ 2 + r ^ 2)(也是随机生成的)

These are using the formulas for a circle: radius: r=sqrt(x^2+y^2) sphere:r=sqrt(x^2+y^2+z^2) 这些是使用圆的公式:radius:r = sqrt(x ^ 2 + y ^ 2)sphere:r = sqrt(x ^ 2 + y ^ 2 + z ^ 2)

I would be incredibly grateful if you could answer any part of my question, thank you so much for taking the time to read this!!! 如果你能回答我问题的任何部分,我将非常感激,非常感谢您花时间阅读这篇文章!

**by the way, I am using python x,y spyder! **顺便说一下,我正在使用python x,y spyder!

random.uniform will give you a uniformly distributed random number between a given minimum and maximum. random.uniform将在给定的最小值和最大值之间给出均匀分布的随机数。

Just generate random points and check if they are within the sphere. 只需生成随机点并检查它们是否在球体内。 If they are not, discard them and try again. 如果不是,请丢弃它们并再试一次。

import math
import random

def generate_points(n_points, min_diameter=0, max_diameter=1):
    diameter = random.uniform(min_diameter, max_diameter)
    radius = diameter / 2
    count = 0
    while count < n_points:
        x, y, z = [random.uniform(-radius, radius) for _ in range(3)]
        distance = math.sqrt(x * x + y * y + z * z)
        if distance <= radius:
            yield (x, y, z)
            count += 1

for x, y, z in generate_points(10):
    print x, y, z

Note: this may bias what points are generated. 注意:这可能会偏向生成哪些点。 (I'm not sure, it's probably okay actually.) Another approach might be to use polar coordinates, choosing two random angles and a random radius (offset from center). (我不确定,它实际上可能还行。)另一种方法可能是使用极坐标,选择两个随机角度和一个随机半径(偏离中心)。

Here is the math for that approach: 以下是该方法的数学计算:

theta = random.uniform(0, 2 * math.pi)
phi = random.uniform(-math.pi / 2, math.pi / 2)
x = r * cos(theta) * cos(phi)
y = r * sin(phi)
z = r * sin(theta) * cos(phi)

See here for more on distribution: 有关分发的更多信息,请参见此

https://math.stackexchange.com/questions/87230/picking-random-points-in-the-volume-of-sphere-with-uniform-probability https://math.stackexchange.com/questions/87230/picking-random-points-in-the-volume-of-sphere-with-uniform-probability

http://mathworld.wolfram.com/SpherePointPicking.html (this one is about points on the surface of the sphere, so might not be as useful) http://mathworld.wolfram.com/SpherePointPicking.html (这是关于球体表面上的点,所以可能没那么有用)

I used this for a random generator and it works, i'm not sure if it's the right one you're looking for but I hope it helps 我用它来做一个随机发生器而且它有效,我不确定它是否是你正在寻找的正确的但我希望它有帮助

import random

maths_operator_list=['+','-','*']
maths_operator = random.choice(maths_operator_list)
number_one = random.randint(0,20)
number_two = random.randint(0,20)
correct_answer = 0

print(str(number_one), str(maths_operator), number_two)

if maths_operator == '+':
    correct_answer = number_one + number_two
elif maths_operator == '-':
    correct_answer = number_one - number_two
elif maths_operator == '*':
    correct_answer = number_one * number_two

print(correct_answer)

As answer to the random number generator : 作为随机数发生器的答案:

RN = random.uniform(a, b)

Return a random integer N such that a <= N <= b. 返回一个随机整数N,使得a <= N <= b。

As how to initiate x and y, y will be initialize when you set y = to something, it does it automatically. 至于如何启动x和y,当你将y =设置为某个东西时,y将被初始化,它会自动执行。

You can use random.range 你可以使用random.range

random.randrange(-15,15,.1)

That will find a number between -15 and 15 that is divisible by 0.1. 这将找到介于-15和15之间的数字,可被0.1整除。

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

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