我试图创建一个图像文件,如下所示:
uint8_t raw_r[pixel_width][pixel_height];
uint8_t raw_g[pixel_width][pixel_height];
uint8_t raw_b[pixel_width][pixel_height];
uint8_t blue(uint32_t x, uint32_t y)
{
return (rand()%2)? (x+y)%rand() : ((x*y%1024)%rand())%2 ? (x-y)%rand() : rand();
}
uint8_t green(uint32_t x, uint32_t y)
{
return (rand()%2)? (x-y)%rand() : ((x*y%1024)%rand())%2 ? (x+y)%rand() : rand();
}
uint8_t red(uint32_t x, uint32_t y)
{
return (rand()%2)? (y-x)%rand() : ((x*y%1024)%rand())%2 ? (x+y)%rand() : rand();
}
for (y=0; y<pixel_height; ++y)
{
for (x=0; x<pixel_width; ++x)
{
raw_b[x][y]=blue(x, y);
raw_g[x][y]=green(x, y);
raw_r[x][y]=red(x, y);
}
}
Run Code Online (Sandbox Code Playgroud)
我希望随机得到一些东西(白噪声).但是,输出很有意思:
你知道原因吗?
现在,很明显它与之无关rand().
也试试这段代码: …
这很可能是与机器有关的问题,但我无法弄清楚可能是什么问题。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char** argv) {
srand(time(NULL));
int r1 = rand();
int r2 = rand();
printf("%d %d\n", r1, r2);
}
Run Code Online (Sandbox Code Playgroud)
我编译上面的代码使用
gcc randd.c
Run Code Online (Sandbox Code Playgroud)
然后手动运行几次,第一个数字看起来非常相似,而第二个数字似乎是随机的:
1025720610 1435057801
1025737417 1717533050
1025754224 2000008299
1025771031 134999901
1025787838 417475150
Run Code Online (Sandbox Code Playgroud)
第一次调用rand()似乎与时间密切相关,并且随着时间的推移严格增加。关于为什么会发生这种情况或如何解决它的任何想法?
这发生在 OSX 10.11
我不明白为什么srand()在运行之间生成如此相似的随机数!
我试图运行以下代码
srand ( time(NULL) );
int x = rand();
cout << x << endl;
Run Code Online (Sandbox Code Playgroud)
然而,不是一个适当的随机数,我总是得到几乎相同的数字,随着时间的推移,这个数字正在缓慢增长.所以我得到的数字如:11669,11685,11701,11714,11731.
我究竟做错了什么?
我正在使用Visual Studio 2010 SP1.
好吧,srand()真的那么简单吗?我的意思是如何将其称为随机函数?
srand(1) => rand() = 41
srand(2) => rand() = 45
srand(3) => rand() = 48
srand(4) => rand() = 51
....
Run Code Online (Sandbox Code Playgroud) 这是一个看似常见的问题,所以我希望我听起来并不多余.但是从rand()返回的范围应该在0和RAND_MAX之间,但是,当我做一个非常简单的rand语句时,我总是在很小的范围内得到回报.
此范围类似于1,4XX,XXX,XXX.我认为这可能是一个时钟的东西,所以我等了三十分钟,我仍然得到相同范围内的数字.
以下是20分钟前的一些示例输出:
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439810968
80
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439827775
29
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439827775
29
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439844582
78
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439878196
29
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439895003
78
Run Code Online (Sandbox Code Playgroud)
这是刚刚的示例输出:
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1456483512
78
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1456500319
80
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1456500319
80
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1456517126
29
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1456533933
78
Run Code Online (Sandbox Code Playgroud)
我知道rand()并不完美,但这似乎太相似了.如果范围是0 - RAND_MAX,则返回的每个数字都在相同的范围内似乎很奇怪.
这是我测试的代码:
#include <iostream>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include …Run Code Online (Sandbox Code Playgroud)