C / C++语言中rand() 每次产生的随机数一样
来源:互联网 发布:阿里云和腾讯云对比 编辑:程序博客网 时间:2024/05/23 19:17
C / C++语言中
rand() 每次产生的随机数一样
int rand( void );
[csharp] #include
#include
#include
int main( void )
{
int i;
// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
//
srand( (unsigned)time( NULL ) );
// Display 10 numbers.
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
printf("\n");
// Usually, you will want to generate a number in a specific range,
// such as 0 to 100, like this:
{
int RANGE_MIN = 0;
int RANGE_MAX = 100;
for (i = 0; i < 10; i++ )
{
int rand100 = (((double) rand() /
(double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
printf( " %6d\n", rand100);
}
}
}
#include
#include
#include
int main( void )
{
int i;
// Seed the random-number generator with current time so that
// the numbers will be different every time we run.
//
srand( (unsigned)time( NULL ) );
// Display 10 numbers.
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
printf("\n");
// Usually, you will want to generate a number in a specific range,
// such as 0 to 100, like this:
{
int RANGE_MIN = 0;
int RANGE_MAX = 100;
for (i = 0; i < 10; i++ )
{
int rand100 = (((double) rand() /
(double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
printf( " %6d\n", rand100);
}
}
}
srand() 可使每次产生的随机数不同,和rand连用
[cpp] #include
#include
#include
using namespace std;
int main()
{
srand((unsigned)time(NULL)); //初始化随机数种子
for ( int i = 0; i < 10; i ++ ) //产生10个随机数
{
cout << rand()%10 << endl;
}
return 0;
}
#include
#include
#include
using namespace std;
int main()
{
srand((unsigned)time(NULL)); //初始化随机数种子
for ( int i = 0; i < 10; i ++ ) //产生10个随机数
{
cout << rand()%10 << endl;
}
return 0;
}
Objective-C语言中
arc4random() 比较精确不需要生成随即种子
使用方法:
[cpp] arc4random() //随机产生任何数
arc4random()%x //产生0~x之间的随机数
(arc4random()%x )+1 //产生1~x之间的随机数
arc4random() //随机产生任何数
arc4random()%x //产生0~x之间的随机数
(arc4random()%x )+1 //产生1~x之间的随机数
random() 需要初始化时设置种子
使用方法:
[cpp] srandom((unsigned int)time(time_t *)NULL); //初始化时,设置下种子就好了。
srandom((unsigned int)time(time_t *)NULL); //初始化时,设置下种子就好了。
- C / C++语言中rand() 每次产生的随机数一样
- rand()每次产生的随机数都一样
- 为什么rand()每次产生的随机数都一样
- C语言产生随机数,每次产生的不一样
- C语言中随机数产生 rand和srand
- C语言产生随机数 srand rand
- 利用srand,rand产生随机数-C语言
- C语言如何产生一个随机数,rand函数的使用
- C语言<time.h>中srand(),rand()等等函数产生随机数的用法。
- C/C++中产生随机数(rand和srand的用法)
- C/C++中产生随机数函数(rand,srand)的用法
- c语言:随机数rand()
- C/C++ rand产生随机数
- c++rand()函数-产生随机数
- C语言中产生随机数的方法
- C语言中产生随机数的方法
- C语言中产生随机数的办法
- C语言中随机数的产生
- 打印机选购
- HDOJ 1175 连连看
- JVM——基础概念总结:数据类型、堆与栈
- oracle中导入dmp字符乱码分析和解决方案
- Init.ora Parameter "_DISABLE_LOGGING" [Hidden
- C / C++语言中rand() 每次产生的随机数一样
- 黑马程序员---- java中 equals 和 ==的区别
- commons-lang(time应用)
- android am
- Selenium学习资料
- Cookie的简单使用
- 关于IOS中与JavaScript的交互
- Android Paint类的介绍
- C++虚函数与虚函数表(四)