【问题标题】:Python - Generate random real number between range with a step sizePython - 在具有步长的范围之间生成随机实数
【发布时间】:2018-10-20 17:59:10
【问题描述】:

我正在使用 python-3.x,我正在尝试生成一个初始总体,其中包含 0 到 1 之间的随机实数,其中这些数字应为以下之一: 0、0.33333、0.666667 或 1

这意味着这些数字之间的差异是 0.33333 (1/3)。我尝试以多种方式修改此代码,但没有运气

import numpy as np
import random
from random import randint
from itertools import product
pop_size = 7
i_length = 2
i_min = 0
i_max = 1
level = 2
step = ((1/((2**level)-1))*(i_max-i_min))

def individual(length, min, max):
    return [ randint(min,max) for x in range(length) ]
def population(count, length, min, max):
    return [ individual(length, min, max) for x in range(count) ]

population = population(pop_size, i_length, i_min, i_max)

##count: the number of individuals in the population
##length: the number of values per individual
##min: the minimum possible value in an individual's list of values
##max: the maximum possible value in an individual's list of values
##this code was taken from :https://lethain.com/genetic-algorithms-cool-name-damn-simple/

我做了这几行,对我来说效果很好:

population2 = np.array(list(product(np.linspace(i_min, i_max, 2**level), repeat=2)))
population3 = [j for j in product(np.arange(i_min, i_max+step, step), repeat=2)]

但问题是它会列出所有可能不是我想要的值。我想要给出人口规模的随机数

我想看到的结果是 smailar to (numpy array or list):

population = [[0, 1],
             [0, 0.3333],
             [0.3333, 1],
             [1, 0.6667],
             [0.3333, 0.6667],
             [0.6667, 0],
             [0.3333, 0.3333]]

请记住:

level = 2

我可以在哪里计算步长值:

step = ((1/((2**level)-1))*(i_max-i_min))

例如,如果我将 level = 2 更改为 level = 3,则不再使用 0.3333,它将更改为 0.1428 1/7),我将获得不同的值。

任何建议将不胜感激

【问题讨论】:

    标签: python python-3.x list numpy random


    【解决方案1】:
    >>> np.random.choice([0, 1/3., 2/3., 1], size=(7,2), replace=True)
    array([[0.        , 0.33333333],
           [0.33333333, 0.66666667],
           [0.        , 0.        ],
           [0.66666667, 0.        ],
           [0.33333333, 0.33333333],
           [1.        , 1.        ],
           [0.33333333, 0.33333333]])
    
    
    >>> i_min = 0
    >>> i_max = 1
    >>> level = 3
    >>> np.random.choice(np.linspace(i_min, i_max, 2**level), size=(7,2), replace=True)
    array([[0.28571429, 0.14285714],
           [0.85714286, 0.57142857],
           [0.71428571, 0.42857143],
           [0.71428571, 1.        ],
           [0.14285714, 0.85714286],
           [0.        , 0.        ],
           [1.        , 0.        ]])
    

    【讨论】:

    • 感谢您的回复,但您的想法存在问题,它不使用可变步长。例如,如果我将 level = 2 更改为 level = 3 则不再使用 0.3333 它将更改为 0.1428 1/7)
    • @azeez 我使用了一个固定列表,但np.linspace 生成的数组也可以。例如。 np.random.choice(np.linspace(0,1,2**level), size=(7,2), replace=True)
    【解决方案2】:

    没有 numpy:

    from random import randint
    
    def get_population(num, repeats, fraction):
     return [[randint(0, fraction)/fraction for x in range(num)] for i in range(repeats)]
    
    print(get_population(2, 7, 3))
    

    输出是:

    [[0.3333333333333333, 0.0],
     [0.3333333333333333, 1.0],
     [1.0, 0.3333333333333333],
     [0.3333333333333333, 0.0],
     [0.0, 0.3333333333333333],
     [0.3333333333333333, 0.6666666666666666],
     [1.0, 1.0]]
    

    分数 7:

    print(get_population(2, 7, 7))
    

    输出是:

    [[0.8571428571428571, 0.7142857142857143],
     [0.7142857142857143, 0.14285714285714285], 
     [0.0, 0.7142857142857143], 
     [0.42857142857142855, 0.5714285714285714], 
     [0.42857142857142855, 0.7142857142857143], 
     [1.0, 0.5714285714285714], 
     [1.0, 1.0]]
    

    【讨论】:

    • 谢谢@Konstantin 的回复,如果我改了步值level = 2 to level = 3
    • 只需将randint(0, 3)/3编辑成randint(0, fraction)/fraction,就可以随意修改了
    • 这里的fraction是什么意思,是指fractions模块吗?
    • 不,它只是一个变量。分数可以是随机整数,例如 randint(0, 7)/7 表示 1/7 步长。帖子编辑
    • 当然您也可以将fraction 变量替换为您的(2**level)-1 语法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    相关资源
    最近更新 更多