Chinaunix首页 | 论坛 | 博客
  • 博客访问: 399687
  • 博文数量: 83
  • 博客积分: 2011
  • 博客等级: 大尉
  • 技术积分: 741
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-04 22:51
文章分类

全部博文(83)

文章存档

2009年(83)

我的朋友

分类: LINUX

2009-07-18 16:17:41

rand()返回一个 0- RAND_MAX(2147483647)之间的随机数
产生一个 X , X >=0, X<1, 可用rand()/(RAND_MAX + 1.0)
 
以下是个简单的程序:
#include
#include
#include
int main()
{
        int i, j;
        srand((int)time(NULL));
        for (i = 0; i < 10; i++)
        {
                j = 1 + (int)(10.0 * rand() / (RAND_MAX + 1.0));
                printf("%d ", j);
        }
 printf("\n");
}
编译: gcc 3-3.c -o 3-3
[root@localhost math]# ./3-3
3 5 5 8 1 6 10 7 1 4
[root@localhost math]# ./3-3
9 8 4 3 9 3 1 6 5 6
[root@localhost math]# ./3-3
7 9 9 4 5 9 9 1 5 6
[root@localhost math]#
 
 
阅读(1546) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~