Python - 使用步长在范围之间生成随机实数

时间:2022-12-25 04:22:24

I am using python-3.x, and I am trying to generate an initial population that contains random real numbers between 0 and 1 where these numbers should be one of the following: 0, 0.33333, 0.666667 or 1

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

That means the difference between these numbers is 0.33333 (1/3). I tried to modify this code in many ways but their no luck

这意味着这些数字之间的差异是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/

I did this lines which works very well for me:

我做了这些对我有用的线路:

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)]

but the problem it will list all the possible values which are not what I want. I want random numbers where the population size will be given

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

the result I want to see is smailar to (numpy array or list):

我想看到的结果是smailar to(numpy数组或列表):

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

keep in mind the:

请记住:

level = 2

where I can calculat the the step value:

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

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

for example, if I changed the level = 2 to level = 3 then it is no more using the 0.3333 it will change to 0.1428 1/7) which I will get different values.

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

Any advice would be much appreciated

任何建议将不胜感激

2 个解决方案

#1


3  

>>> 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.        ]])

#2


0  

Without 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))

Output is:

[[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]]

Fraction 7:

print(get_population(2, 7, 7))

Output is:

[[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]]

#1


3  

>>> 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.        ]])

#2


0  

Without 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))

Output is:

[[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]]

Fraction 7:

print(get_population(2, 7, 7))

Output is:

[[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]]