rand
来源:互联网 发布:原型图软件 编辑:程序博客网 时间:2024/04/30 07:22
#include<stdio.h>#include<stdlib.h>#include<time.h>int main(){int i;srand(time(NULL));for(i = 0; i < 100; i++)printf("%d\t",rand() % 10);return 0;}
rand(产生随机数)
相关函数
srand
表头文件
#include<stdlib.h>
定义函数
int rand(void)
函数说明
rand()会返回一随机数值,范围在0至RAND_MAX 间。在调用此函数产生随机数前,必须先利用srand()设好随机数种子,如果未设随机数种子,rand()在调用时会自动设随机数种子为1。关于随机数种子请参考srand()。
返回值
返回0至RAND_MAX之间的随机数值,RAND_MAX定义在stdlib.h,其值为2147483647。
范例
/* 产生介于1 到10 间的随机数值,此范例未设随机数种子,完整的随机数产生请参考
srand()*/
#include<stdlib.h>
main()
{
int i,j;
for(i=0;i<10;i++)
{
j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
printf("%d ",j);
}
}
执行
9 4 8 8 10 2 4 8 3 6
9 4 8 8 10 2 4 8 3 6
srand(设置随机数种子)
相关函数
rand
表头文件
#include<stdlib.h>
定义函数
void srand (unsigned int seed);
函数说明
srand()用来设置rand()产生随机数时的随机数种子。参数seed必须是个整数,通常可以利用geypid()或time(0)的返回值来当做seed。如果每次seed都设相同值,rand()所产生的随机数值每次就会一样。
返回值
范例
/* 产生介于1 到10 间的随机数值,此范例与执行结果可与rand()参照*/
#include<time.h>
#include<stdlib.h>
main()
{
int i,j;
srand((int)time(0));
for(i=0;i<10;i++)
{
j=1+(int)(10.0*rand()/(RAND_MAX+1.0));
printf(" %d ",j);
}
}
执行
5 8 8 8 10 2 10 8 9 9
2 9 7 4 10 3 2 10 8 7
rand() % 10 产生 0 ~9的数字
0 0
- rand
- rand
- rand()
- Rand Number
- rand() 函数
- rand问题
- rand ()函数
- rand用法
- rand & srand
- rand函数
- rand()随机数
- rand函数
- rand函数
- rand函数
- 笔记--rand()
- rand()函数
- rand()&&srand()
- srand rand
- 视频720P和1080P是什么意思
- Flatten Binary Tree to Linked List
- iOS开发笔记--UILabel显示不同的字体和颜色
- 康托展开深入淺出【改】
- ajax中关于POST和GET
- rand
- id ,NSObject, id<NSObject>区别
- Phalcon apache .htaccess 重写
- iOS开发笔记--UIImageView的属性之animationImages详解
- Java:对象与垃圾回收
- @autoreleasepool内存管理
- 虚拟机VMware里 windows server 2003 扩充C盘方法
- cakephp在linux或者mac上部署的重定向问题
- Storing Videos in the Photo Library(储存视频至手机)