C++ 生成不同的随机坐标
所以我有一个由C++ 生成不同的随机坐标,c++,random,coordinates,C++,Random,Coordinates,所以我有一个由向量构成的“*”平方。现在我需要生成六个数字,它们代表从1到N*N的不同坐标(这是正方形的面积),这样我就可以修改它们。到目前为止,我得到的是这个 #include <cstdlib> #include <iostream> int main(){ int N = 4, x, y; srand(time(NULL)); for(int i = 0; i < 6; i++){ //generate
向量
构成的“*”平方。现在我需要生成六个数字,它们代表从1到N*N的不同坐标(这是正方形的面积),这样我就可以修改它们。到目前为止,我得到的是这个
#include <cstdlib>
#include <iostream>
int main(){
int N = 4, x, y;
srand(time(NULL));
for(int i = 0; i < 6; i++){
//generate 6 random coordinates
int rnd = rand() % (N*N-1) + 2;
if(rnd <= N){
x = rnd;
y = 0;
}
else{
if((x = (rnd % N) - 1) < 0){
x = rnd / N - 1;
y = x;
}
else y = rnd / N;
}
std::cout << "rnd: " << rnd << " x:" << x << " y:" << y << "\n\n";
}
}
#包括
#包括
int main(){
int N=4,x,y;
srand(时间(空));
对于(int i=0;i<6;i++){
//生成6个随机坐标
int rnd=rand()%(N*N-1)+2;
如果(rnd我每次运行这个时都会得到不同的结果。但是请注意,时间(NULL)每秒只更改一次,因此在同一时间段内运行两次会产生相同的随机种子
然而,你的代码还有很多其他问题,你的数学是错误的
(一)
这将始终产生至少2的结果,因此您永远不会得到1的结果,而代码的其余部分似乎打算引用网格的左上角。因此,一开始,您的随机公式是倾斜的
(二)
网格中有N*N个坐标
rand() % *value*
除以“值”(假定该值为正)的余数总是产生一个介于0和(值-1)之间的结果,包括0和(值-1)。毕竟:
0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
因此:
这将始终产生一个介于0和N*N-2之间的结果。因此,如果N是4,N*N-1是15,并且这将始终是一个介于0和14之间的结果,包括0和14,或者对于随机坐标,只有15个可能的值,而网格中确实有16个不同的坐标
rand() % *value*
结论:
你的方法的整体问题源于人类自然倾向于从1开始,然后是2,等等;在处理计算机代码和算法时,更自然的是从0开始,而不是1
当这种情况发生时,整个随机坐标生成变得非常简单:
int rnd = rand() % (N*N);
int x= rnd % N;
int y= rnd / N;
就这样。不需要那种笨拙的if
装置了。其他答案讨论由srand(时间(空))引起的问题。
那么
无论两次调用之间的时间间隔如何,都可以确保生成随机数
你可以用像
int Min, Max;
random_device rd; // non-deterministic generator
mt19937 gen(rd()); // to seed mersenne twister.
uniform_int_distribution<> dist(Min,Max); // distribute results between Min and Max inclusive.'
别忘了包括
<random>
为了得到x
和y
,你做了一些奇怪的事情。这比你得到的要简单得多:
for(int i = 0; i < 6; i++){
// the square is N x N, so you want two numbers from 0 to N-1
int x = rand() % N;
int y = rand() % N;
std::cout << "x:" << x << " y:" << y << "\n\n";
}
或者:
for(int i = 0; i < 6; i++){
int rnd = rand() % (N*N);
int x = rnd % N;
int y = rnd / N;
std::cout << "rnd: " << rnd << " x:" << x << " y:" << y << "\n\n";
}
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 eng(seed);
std::uniform_int_distribution<> dist(0, N*N - 1);
for(int i = 0; i < 6; i++) {
int rnd = dist(eng);
int x = rnd % N; // used of % here is just fine
int y = rnd / N;
std::cout << "rnd: " << rnd << " x:" << x << " y:" << y << "\n\n";
}
它可以工作(做一些明显的伪随机).srand
每秒只会更改一点值,因此如果您连续短时间运行此代码两次,我希望您确实会得到相同/类似的结果。这与“随机种子”的方式有关工作-你的种子在下一秒只会从111318789变为111318790,这并不是说它会扰乱数字中的每一位。你可以尝试使用“获得纳秒时间”,或者你可以在两次跑步之间再等一段时间…@马特森会尝试的that@0d0a它产生了7个3times@MatsPetersson我怎么能得到纳秒级的时间,在谷歌上没有找到任何东西,第一个坐标和最后一个坐标都被取下了,这就是我加上2的方式。这里有一些尴尬的情况,因为这些坐标与向量一起使用,所以v[y][x]给我省了不少麻烦,谢谢:)这就是我要找的,谢谢
for(int i = 0; i < 6; i++){
int rnd = rand() % (N*N);
int x = rnd % N;
int y = rnd / N;
std::cout << "rnd: " << rnd << " x:" << x << " y:" << y << "\n\n";
}
#include <random>
std::mt19937 eng; // object which produces random bits
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
eng.seed(seed); // seed the bit generator, replaces srand()
std::uniform_int_distribution<> dist(0, N-1); // encapsulates the correct method of turning
// random bits into random numbers in the range
// [0, N)
for(int i = 0; i < 6; i++) {
int x = dist(eng); // the distribution internally used the engine like rand()
int y = dist(eng);
std::cout << "x:" << x << " y:" << y << "\n\n";
}
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 eng(seed);
std::uniform_int_distribution<> dist(0, N*N - 1);
for(int i = 0; i < 6; i++) {
int rnd = dist(eng);
int x = rnd % N; // used of % here is just fine
int y = rnd / N;
std::cout << "rnd: " << rnd << " x:" << x << " y:" << y << "\n\n";
}
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
x:0 y:7
x:0 y:2
x:6 y:8
x:5 y:0
x:4 y:7
x:0 y:2
***** ****
**********
*********
**********
**********
**********
**********
*** *****
****** ***
**********