C++ 随机未在范围内声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13568426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Random not declared in scope
提问by soniccool
I am getting the error:
我收到错误:
tester.cpp|20|error: 'rand' was not declared in this scope|
tester.cpp|20|错误:'rand' 未在此范围内声明|
Did I miss to include something here?
我错过了在这里包含的东西吗?
void tester::volumeset(bool A_B, int i, int v)
{
if (A_B == true)
{
A_volumn[i] = rand(v+1);
}else{
B_volumn[i] = rand(v+1);
}
}
回答by Fred Foo
random
is not a standard C++ function; it's a POSIXfunction, so it's not available on Windows. Use rand
instead, or better, the new C++11 randomness library.
random
不是标准的 C++ 函数;它是POSIX函数,因此在 Windows 上不可用。改用rand
或更好地使用新的C++11 随机性库。
回答by Rishabh Agrahari
rand
is part of cstdlib
, try including cstdlib
in your code.
rand
是 的一部分cstdlib
,请尝试包含cstdlib
在您的代码中。
#include <cstdlib>
#include <cstdlib>
or
或者
#include <stdlib.h>
#include <stdlib.h>
回答by Ari
You want to use rand()
, not random()
. You can see some examples of how to use it here: http://www.cplusplus.com/reference/cstdlib/rand/
你想用rand()
,不是random()
。您可以在此处查看有关如何使用它的一些示例:http: //www.cplusplus.com/reference/cstdlib/rand/
回答by AJcleverprogrammer
when i compile my code with Code::Blocks i don't need to #include <cstdlib>
, with DevC++ i simply added #include <cstdlib>
and it works for me.
当我用 Code::Blocks 编译我的代码时,我不需要#include <cstdlib>
,我只是添加了 DevC++,它对我有用#include <cstdlib>
。
回答by Tesfahunegn Minwuyelet
enter code here
only including #include or #include may not work it is better to use for instance I get a challenge when I use rand(100) but instead of this when I use rand()%100 it works well. try and enjoy it.
enter code here
仅包含 #include 或 #include 可能不起作用,例如使用 rand(100) 时我遇到了一个挑战,但是当我使用 rand()%100 时,它运行良好。尝试并享受它。
for(i=0;i<cust;i++)
{
rsr[i]=rand(100);
}
it works when I change it to
for(i=0;i<cust;i++)
{
rsr[i]=rand()%100;
}
if you want to make it 1-100 use below but 0-99 use the above
如果你想让它 1-100 使用下面但 0-99 使用上面
for(i=0;i<cust;i++)
{
rsr[i]=rand()%(100+1);
}
for(i=0;i<cust;i++)
{
rsr[i]=rand()%100+1;
}