我的问题是:如何在numpy中生成非重复的随机数?
list = np.random.random_integers(20,size=(10))
Run Code Online (Sandbox Code Playgroud) 我想生成1到4,4范围内的随机数,包括.
这是我的代码:
int num = r.nextInt(4) + 1; //r is instance of Random.
Run Code Online (Sandbox Code Playgroud)
但是,我在循环中运行上面的代码,不想重复随机数.现在发生的事情通常是我得到的:
1,1,1,2,3,1,4,2,2,1,4,2,4,4,2,1,4,3,3,1,4,2,4,1
作为我的输出.
这里,虽然数字在范围(1-4)内是随机的,但是经常像前3次迭代中的数字"1"那样重复.
我正在寻找的是一种在循环中获得非重复随机数的方法.我知道的一个简单方法是在当前迭代之前保留最后一个随机数并进行比较,但我相信必须有更好的解决方案.
提前致谢.
我的目的是从1到9生成随机数而不重复
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
int randrange(int low,int high) /* generates a random number within given range*/
{
return rand()%(low+high)+low+1;
}
int main()
{
int num[9]={0},i,j;
bool check;
for(i=0;i<9;i++)
{
check=false;
do
{
num[i]=randrange(1,9);
for(j=0;j<i;j++)
{
if( num[i]==num[j]) // checks whether number already exists in the array
check=false;
else
check=true;
}
} while(check==false);
}
// the program is working fine without the repetition check
// this section prints out the array elements
for(i=0;i<9;i++)
{
cout<<num[i]<<" ";
}
return …
Run Code Online (Sandbox Code Playgroud) 我需要生成大约9到1亿个非重复随机数,范围从零到生成的数字量,我需要它们很快生成.对类似问题的几个答案提出简单地改组数组以获得随机数,而其他提议使用布隆过滤器.问题是,哪一个更有效,如果它是布隆过滤器,我该如何使用它?
我正在尝试创建一个尽可能高效的速度距离和时间计算器,并且想要使用根据输入而变化的指针来调用函数,但我不确定如何这样做.我尝试了很多不同的东西:
我最好的尝试:
// Inside class Math
double calcSpeed(double distance, double time); // These functions return a double value of the formula (non static)
double calcDistance(double speed, double time);
double calcTime(double speed, double distance);
// Inside Main
typedef double (Math::*FuncChosen)(double first, double second);
FuncChosen p = &Math::calcSpeed; // This changes according to input to different functions in Math class with same parameter types and return types
p(1, 2); // Not a function, how can I non explicitly call the function?
Run Code Online (Sandbox Code Playgroud)
有没有办法调用此函数,而无需使用指针或其他方式明确引用它.就像从根据输入改变的指针调用函数一样?我真的不知道从哪里开始和使用什么,因为我尝试的一切都是非法的.我基本上想在运行时选择该函数而不使用几个ifs,从而避免重复.(我发现有类似问题的人,但没有找到一种有效的方法来实现我的目的.) …
我正在寻找一种有效的算法,可以在一个范围内产生随机值,而不需要重复.
在伪代码中:(在Rand类中)
Rand(long from, long to) {
this.from = from;
this.to = to;
// ...
}
long getNumber() {
// returns a random number in the [from, to] range
// which has never been returned before
}
Run Code Online (Sandbox Code Playgroud)
用法:
Rand r = new Rand(1, 100000000);
long x = r.getNumber();
long y = r.getNumber();
...
Run Code Online (Sandbox Code Playgroud)
从r.getNumber()返回的数字应始终与先前返回的数字不同.
当然,如果to - from + 1
返回数字,算法应该重新开始(或者错误 - 不管怎么说都不重要).
注意,范围可能非常大,因此随机排列的数组(最初包含[from, to]
数字)可能会溢出存储器.
我正在寻找允许构建更灵活场景的机制。
例如,对于测试数据库中记录是否存在的这两个非常相似的场景:
Scenario Outline: Testing query with 1 attribute with these 2 record in and another 2 out of result
Given I'm connected to <db> database
When I select <query> from database
Then Result should contain fields:
| <row> |
| <yes1> |
| <yes2> |
And Result should not contain fields:
| <row> |
| <no1> |
| <no2> |
Examples:
| db | row | yes1 | yes2 | no1 | no2 | query |
| 1 | …
Run Code Online (Sandbox Code Playgroud)