我试过用random.randint(0, 100)
,但有些数字是一样的.是否有方法/模块来创建列表唯一的随机数?
def getScores():
# open files to read and write
f1 = open("page.txt", "r");
p1 = open("pgRes.txt", "a");
gScores = [];
bScores = [];
yScores = [];
# run 50 tests of 40 random queries to implement "bootstrapping" method
for i in range(50):
# get 40 random queries from the 50
lines = random.sample(f1.readlines(), 40);
Run Code Online (Sandbox Code Playgroud) 我想为测试目的创建一个随机的整数列表.数字的分布并不重要.唯一重要的是时间.我知道生成随机数是一项耗时的任务,但必须有更好的方法.
这是我目前的解决方案:
import random
import timeit
# Random lists from [0-999] interval
print [random.randint(0, 1000) for r in xrange(10)] # v1
print [random.choice([i for i in xrange(1000)]) for r in xrange(10)] # v2
# Measurement:
t1 = timeit.Timer('[random.randint(0, 1000) for r in xrange(10000)]', 'import random') # v1
t2 = timeit.Timer('random.sample(range(1000), 10000)', 'import random') # v2
print t1.timeit(1000)/1000
print t2.timeit(1000)/1000
Run Code Online (Sandbox Code Playgroud)
v2比v1快,但它的工作规模不大.它给出以下错误:
ValueError:大于人口的样本
是否有一个快速,有效的解决方案,以这种规模工作?
安德鲁:0.000290962934494
gnibbler's:0.0058455221653
KennyTM's:0.00219276118279
NumPy来了,看到并征服了.
这可能是一个愚蠢的问题,但我无法找到一个函数来生成一定范围内给定长度的随机浮点数组.
我看过随机抽样,但没有功能似乎做我需要的.
random.uniform接近但它只返回一个元素,而不是一个特定的数字.
这就是我所追求的:
ran_floats = some_function(low=0.5, high=13.3, size=50)
Run Code Online (Sandbox Code Playgroud)
这将返回[0.5, 13.3]
在该范围内均匀分布的随机非唯一浮点数组(即:允许重复)[0.5, 13.3]
.
有这样的功能吗?
我在这里有一些东西,但我无法按照自己喜欢的方式工作:
def nested_loops():
import random
option1 = random.randint(1, 3)
option2 = random.randint(1, 3)
option3 = random.randint(1, 3)
Run Code Online (Sandbox Code Playgroud)
上面的位产生数字但它们可能是相同的.在这下面可以解决这个问题,但它没有,但它似乎只是降低了可能性
while option1 == option2:
option1 = random.randint(1,3)
while option1 == option3:
option1 = random.randint(1, 3)
while option2 == option3:
option2 = random.randint(1, 3)
print(option1)
print(option2)
print(option3)
Run Code Online (Sandbox Code Playgroud)
相当明显它只是打印它们
所以我有一个整数列表,如下所示:
list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
我想从中选择随机整数:
item = random.choice(list)
Run Code Online (Sandbox Code Playgroud)
但是我如何确保下次这样做时,它是不同的项目?我不想从列表中删除项目.
我需要在C++中生成随机非重复数组,在这部分代码中我使用srand函数生成随机数,但有些数字是重复的.主要任务是为彩票生成随机数,所以我需要生成数字,直到标记为int golden的黄金数字.
Run Code Online (Sandbox Code Playgroud)#include <cstdlib> #include <ctime> #include <iostream> using namespace std; int main() { int golden = 31; int i = 0; int array[35]; srand((unsigned)time(0)); while(i != golden){ array[i] = (rand()%75)+1; cout << array[i] << endl; i++; } }
我有以下列表:
a = [1, 2, 5, 4, 3, 6]
Run Code Online (Sandbox Code Playgroud)
而且我想知道如何在列表中随机交换任意两个值,而不管列表中的位置如何.以下是我正在考虑的一些示例输出:
[1, 4, 5, 2, 3, 6]
[6, 2, 3, 4, 5, 1]
[1, 6, 5, 4, 3, 2]
[1, 3, 5, 4, 2, 6]
Run Code Online (Sandbox Code Playgroud)
有没有办法在Python 2.7中执行此操作?我目前的代码是这样的:
import random
n = len(a)
if n:
i = random.randint(0,n-1)
j = random.randint(0,n-1)
a[i] += a[j]
a[j] = a[i] - a[j]
a[i] -= a[j]
Run Code Online (Sandbox Code Playgroud)
然而,我目前拥有的代码的问题是,如果给出足够的交换和迭代,它会开始将所有值设置为零,这是我不想要的; 我希望值在数组中保持不变,但是执行类似于2opt的操作,并且每次交换时只切换两次.
我想生成一个随机数,该随机数应该是唯一的,这意味着它不应该重复或重新生成.我已经尝试制作一个列表,我在每次迭代中追加并检查它是否已经存在于列表中.如果它出现在列表中,则不会附加该数字.但我不认为这是一种有效的方法.所以,请帮助我.
import random
#an empty list to append randomly generated numbers
empty = []
while True:
#checking from range 500 to 510
number = random.randrange(500, 510)
if number not in empty:
empty.append(number)
else:
pass
#breaking the loop if the length of the numbers is 9
if len(empty) == 9:
break
print("Final list --> " + str(empty))
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个随机选择数字并将它们添加到列表中的代码random_numbers
,但是如果已经生成了一个随机数,则代码会检测到并用另一个数字替换该数字,直到每个数字都不同。
import random
random_numbers = []
for x in range(11):
Run Code Online (Sandbox Code Playgroud)
这部分生成一个随机整数并将其附加到列表中random_numbers
:
random_numbers.append('[q' + str(random.randint(1, 11)) + ']')
Run Code Online (Sandbox Code Playgroud)
这部分应该遍历列表并检查生成的随机数是否已经生成,并替换它:
for item in range(len(random_numbers)):
if random_numbers[x] == random_numbers[item]:
random_numbers[x] = '[q' + str(random.randint(1, num_of_qs_in_file)) + ']'
print(random_numbers)
Run Code Online (Sandbox Code Playgroud)
输出会有所不同,但几乎总是列表多次具有相同的整数。有人可以帮忙吗?
我想使用 python 打印随机数。如果我使用 random.randint(1, 10) 它会给我 1-10 的随机数。如果我想打印 1-10 之间的随机数但不包括数字 2 怎么办?谢谢你
python ×9
random ×6
list ×3
arrays ×2
c++ ×1
for-loop ×1
numpy ×1
performance ×1
python-2.7 ×1
python-3.x ×1