Chinaunix首页 | 论坛 | 博客
  • 博客访问: 724113
  • 博文数量: 251
  • 博客积分: 10367
  • 博客等级: 上将
  • 技术积分: 2750
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-10 14:43
文章分类

全部博文(251)

文章存档

2009年(2)

2008年(86)

2007年(163)

分类: C/C++

2007-12-15 00:33:11

/*
  Name: 产生两个数之间的 随机数
  Date: 15-12-07 00:31
  Description: 返回a和b之间的随机数,用时间做种子
*/


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//返回a和b之间的随机数,用时间做种子

int rand_between_a_b(int a,int b)
{
     int c ,re,temp;
     time_t t;
     
     if(a < b)
     {
          temp = a;
          a = b;
          b = temp;
     }
     
     c = a - b;
     srand((unsigned) time(&t));//使用时间做种子数

     re = b + (rand() % c);
     
     return re;
}
int main()
{
    int re;
      
    re = rand_between_a_b(35,98);
    
    printf("%d\n",re);
    getch();
    return 0;
}

 

   当然在linux中还有更好的产生随机数的方法,它有一个内核随机池,记录了像键盘输入之类的随机事件,然后用一定的方法产生随机数,效果应该很好,等有时间了再学习学习它的方法.

阅读(792) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~