rand() 不根据 xcode 中的模数给出随机数 [英] rand() not giving random numbers depending on modulo in xcode
问题描述
我有一个包含 7 个元素的数组,我正在尝试获取 0 - 6 之间的随机数,以便我可以随机选择数组中的一个元素.
#include #include #include <ctime>使用命名空间标准;类颜色{民众:颜色(){颜色[0] = "红色";颜色[1] =橙色";颜色[2] = "黄色";颜色[3] = "绿色";颜色[4] = "蓝色";颜色[5] =靛蓝";颜色[6] =紫罗兰色";}无效的打印颜色(){for (int i = 0; i(time(0)));颜色颜色对象;colorObject.printRandomColor();返回0;}
当我做 rand() % 7
时,我一直得到 6,但如果我做 rand() % 6
我最终得到随机数.什么给?
我在 main() 中调用 srand(static_cast
我注意到问题中显示的代码有相同的行为:
rand() % 7//总是显示 6rand() % 14//总是显示 6 或 13rand() % 21//总是显示 6、13 或 20
这个问题很奇怪,似乎涉及到某种模式.基于有些人无法重现它的评论,我决定编译代码,在基于 Linux 的机器上使用 gcc,在 macOS 上使用 clang;据我所知,Linux 似乎表现正常,但 macOS 则不然.我什至尝试了完全不同的代码,只是确保它不是别的东西,但得到了相同的结果.
#include #include #include <ctime>int main(){int min = 1;整数最大值 = 7;std::srand(std::time(0));//使用当前时间作为随机生成器的种子//int random_variable = std::rand() % max;//总是返回 6//int random_variable = std::rand() % (max - min) + min;//根据时间产生可预测"的数字.int random_variable = RAND_MAX % std::rand() % (max-min) + min;//还根据时间返回谓词结果,但反向除外.std::cout <<[0"上的随机值<<RAND_MAX<<"]:"<<随机变量<<'\n';}
我能够从 rand()
获得看似随机的结果的唯一方法是:
RAND_MAX % std::rand() % (max-min) + min;//基于时间可预测
这个问题很奇怪,可能是 Clang 的一个错误;我不知道这里到底发生了什么.我可能会建议使用 rand()
以外的其他东西,例如评论中提到的
库.
编辑:在向 Apple 报告此错误后,回复如下:
<块引用>Apple 开发者关系 2017 年 7 月 27 日,上午 11:27
没有计划根据以下内容解决此问题:
std::rand 直接使用 C 库中的 rand.rand 是众所周知的,并且记录被破坏(并且不会改变,因为人们取决于它的具体行为).
来自手册页:RAND(3) BSD 库函数手册
姓名rand, rand_r, srand, sranddev -- 错误的随机数生成器
描述这些接口已被 arc4random(3) 废弃.
对于 C++ 中好的伪随机数,请查看 C++11.例如:http://en.cppreference.com/w/cpp/numeric/random
根据此信息,RAND()
已损坏且无法修复 — 使用替代随机数生成器.
I have an array with 7 elements and I'm trying to get a random number between 0 - 6 so I can select an element in the array at random.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Color{
public:
Color(){
colors[0] = "red";
colors[1] = "orange";
colors[2] = "yellow";
colors[3] = "green";
colors[4] = "blue";
colors[5] = "indigo";
colors[6] = "violet";
}
void printColors()
{
for (int i = 0; i<sizeof(colors)/sizeof(colors[0]); ++i)
{
cout << colors[i] << endl;
}
}
void printRandomColor()
{
int random_integer = rand() % 7;
cout << random_integer << endl;
}
private:
string colors[7];
};
int main(int argc, const char * argv[]) {
srand( static_cast<unsigned int>(time(0)));
Color colorObject;
colorObject.printRandomColor();
return 0;
}
When I do rand() % 7
I keep getting 6, but if I do rand() % 6
I end up getting random numbers. What gives?
I call srand( static_cast<unsigned int>(time(0)));
in my main()
I noticed the same behavior with the code shown in the question:
rand() % 7 // always shows 6
rand() % 14 // always shows 6 or 13
rand() % 21 // always shows 6, 13, or 20
The problem is peculiar and there seems to be a pattern involved. Based on the comments that some aren't able to reproduce it, I decided to compile the code, with gcc on a Linux based machine and clang on macOS; Linux seems to behave normally from what I can tell, however macOS does not. I even tried completely different code just make sure it wasn't something else, yet got the same result.
#include <cstdlib>
#include <iostream>
#include <ctime>
int main()
{
int min = 1;
int max = 7;
std::srand(std::time(0)); // use current time as seed for random generator
// int random_variable = std::rand() % max; // always returns 6
// int random_variable = std::rand() % (max - min) + min; // produces 'predictable' numbers based on the time.
int random_variable = RAND_MAX % std::rand() % (max-min) + min; // also returns predicate results based on the timing, except in reverse.
std::cout << "Random value on [0 " << RAND_MAX << "]: "
<< random_variable << '\n';
}
The only way I was able to get seemingly random results from rand()
was to do:
RAND_MAX % std::rand() % (max-min) + min; // predictable based on timing
The issue is odd, and might be a bug with Clang; I'm at a loss at to what exactly is at play here. I would probably recommend using something other than rand()
such as the <random>
library mentioned in the comments perhaps.
EDIT: After reporting this bug to Apple this was the response:
Apple Developer Relations July 27 2017, 11:27 AM
There are no plans to address this based on the following:
std::rand directly uses rand from the C library. rand is known and documented to be broken (and is not going to change since people depend on its specific behavior).
From the man page: RAND(3) BSD Library Functions Manual
NAME rand, rand_r, srand, sranddev -- bad random number generator
DESCRIPTION These interfaces are obsoleted by arc4random(3).
For good pseudorandom numbers in C++, look at from C++11. E.g.: http://en.cppreference.com/w/cpp/numeric/random
Based on this information RAND()
is broken and won't be fixed — use an alternative random number generator.
这篇关于rand() 不根据 xcode 中的模数给出随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!