Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3202342
  • 博文数量: 1805
  • 博客积分: 135
  • 博客等级: 入伍新兵
  • 技术积分: 3345
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-19 20:01
文章分类

全部博文(1805)

文章存档

2017年(19)

2016年(80)

2015年(341)

2014年(438)

2013年(349)

2012年(332)

2011年(248)

分类: C/C++

2015-08-03 13:35:44

原文地址:Linux C伪随机数编程 作者:txgc_wm

一、rand产生伪随机数

Linux中rand()会返回一随机数值,范围在0至RAND_MAX 间,其中RAND_MAX定义在stdlib.h,其值为2147483647。
例一:
  1. #include <stdlib.h>
  2. #include <stdio.h>

  3. #define LOOP_TIMES    10

  4. int main(int argc, char *argv[])
  5. {
  6.     int i;

  7.     for(i=0; i< LOOP_TIMES; i++)
  8.         printf("%d ", rand());

  9.     printf("\n");

  10.     return 0;
  11. }
运行结果:
  1. $ ./rand
  2. 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421
  1. $ ./rand
  2. 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 596516649 1189641421
从以上的结果可以看出,rand两次产生的随机数是一样的,之所以这样是因为“在调用rand函数产生随机数前没有先利用srand()设好随机数种子”。如果未设随机数种子,rand()在调用时会自动设随机数种子为1。


二、srand设置随机数种子

要想每次运行得到的随机数不同,我们还要设置随机数种子。既然要设置随机数种子,那我们就使用srand()来设置rand()产生随机数时的随机数种子。参数seed必须是个整数,通常可以利用geypid()或time(NULL)的返回值来当做seed。如果每次seed都设相同值,rand()所产生的随机数值每次就会一样。
例二:
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>

  4. #define LOOP_TIMES    10

  5. int main(int argc, char *argv[])
  6. {
  7.     int i;

  8.     srand((unsigned int) time(NULL));

  9.     for(i=0; i<LOOP_TIMES; i++)
  10.         printf("%d ", rand());

  11.     printf("\n");

  12.     return 0;
  13. }
运行结果:
  1. $ ./rand
  2. 1943306114 679448932 319436844 1922998560 1458181616 1014851378 1974968086 1165786630 1018064189 616343319
  1. $ ./rand
  2. 1789418334 999331839 757991171 363979956 882919632 1822202571 764206645 468470327 402899724 1322937764

三、合理利用随机数

运行结果是不是不同了,但这么大的数对我们来说也没有多大意义,又怎么利用这些这些随机数呢?我们可对它进行范围化分,这样就可以产生我们想要范围内的数。
产生一定范围随机数的通用表示公式:
    要取得[a,b)的随机整数,使用(rand() % (b-a))+ a;
    要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a,另一种表示为a + (int)b * rand() / (RAND_MAX + 1);
    要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1;
    通用公式:a + rand() % n;其中的a是起始值,n是整数的范围;
    要取得0~1之间的浮点数,可以使用rand() / double(RAND_MAX)。

例三:
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>

  4. #define LOOP_TIMES    10

  5. int main(int argc, char *argv[])
  6. {
  7.     int i, nu;

  8.     srand((unsigned int) time(NULL));

  9.     for(i=0; i< LOOP_TIMES; i++) {
  10.         nu = 0 + (int)( 26.0 *rand()/(RAND_MAX + 1.0));
  11.         printf("%d ", nu);
  12.     }

  13.     printf("\n");

  14.     return 0;
  15. }
运行结果:
  1. $ ./rand
  2. 6 21 20 22 7 18 2 21 14 3
  3. $ ./rand
  4. 4 12 25 3 13 2 0 0 20 12

这个例子主要是生成[0,26]之间的数,使用了语句“nu = 0 + (int)( 26.0 *rand()/(RAND_MAX + 1.0));”来获取a到b之间的随机整数。


四、生成随机字符串

例四:
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <time.h>

  5. #define BUFFER_LENGTH 257

  6. int main(int argc, char *argv[])
  7. {
  8.     int leng = 128 ;
  9.     int i, nu;
  10.     char buffer[BUFFER_LENGTH];

  11.     printf("Please Input length for the String, Default is 128, The Maxest legth is 256:");
  12.     fgets(buffer, BUFFER_LENGTH, stdin);
  13.     buffer[strlen(buffer)-1] = '\0' ;

  14.     if(buffer[0] != '\0')
  15.         leng = atoi(buffer);

  16.     srand((unsigned int)time(NULL));
  17.     bzero(buffer, BUFFER_LENGTH);

  18.     for (i= 0; i< leng; i++)
  19.         buffer[i] = 'a' + ( 0+ (int)(26.0 *rand()/(RAND_MAX + 1.0)));

  20.     buffer[strlen(buffer)] = '\0';

  21.     printf("The randm String is [ %s ]\n", buffer);
  22.     return 0;
  23. }
运行结果:
  1. $ ./rand
  2. Please Input length for the String, Default is 128, The Maxest legth is 256:8
  3. The randm String is [ rdekbnxj ]
  4. $ ./rand
  5. Please Input length for the String, Default is 128, The Maxest legth is 256:36
  6. The randm String is [ bvfrbvvhdcuwdoarefcrkytsntltawpbsusu ]
注意,需要使用“bzero(buffer, 257);”这条语句将缓存清空,否则生成的随机字符串中可能就会出现乱码的情况。


五、shell指令生成字符串

生成全字符随机的字串:
  1. cat /dev/urandom | strings -n C | head -n L
生成数字加字母的随机字串:
  1. cat /dev/urandom | sed 's/[^a-zA-Z0-9]//g' | strings -n C | head -n L
其中C表示字符串的字符数,L表示要生成多少行字符。
阅读(541) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~