随机数
来源:互联网 发布:python recv 编辑:程序博客网 时间:2024/05/29 12:20
C / C++语言中
rand() 每次产生的随机数一样
int rand( void );
#include <stdlib.h>#include <stdio.h>#include <time.h>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连用
#include <iostream>#include <stdlib.h>#include <time.h>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() //随机产生任何数arc4random()%x //产生0~x之间的随机数(arc4random()%x )+1 //产生1~x之间的随机数
random() 需要初始化时设置种子
使用方法:
srandom((unsigned int)time(time_t *)NULL); //初始化时,设置下种子就好了。
- 随机数
- 随机数
- 随机数。。。
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- 随机数
- Ext JS4百强应用: combobox+tree下拉树 支持单选多选级联选择 --第7强
- cuteftp链接不上虚拟机的问题
- springMVC跳转问题
- 讲Linux下面线程的操作、多线程的同步和互斥相关函数及操作的文章链接
- Web 2.0的后继—Web 3.0
- 随机数
- C#多线程解决界面卡死问题的完美解决方案
- jquery实现ul列表中点击radio后面的文本选中radio,点击整个li选中radio
- system cmd
- php性能优化
- windows环境下eclipse for cpp +sdl+boost+mingw环境的搭建
- svn Attempted to lock an already-locked dir
- unsight char 和char 的区别
- Linux 同步方法剖析