C语言生成32位和64位随机数算法

  1. /**
  2. * randstd.h
  3. *
  4. * Standard definitions and types, Bob Jenkins
  5. *
  6. * 2015-01-19: revised by cheungmine
  7. */
  8. #ifndef _RANDSTD_H__
  9. #define _RANDSTD_H__
  10.  
  11. #ifndef STDIO
  12. # include <stdio.h>
  13. # define STDIO
  14. #endif
  15.  
  16. #ifndef STDDEF
  17. # include <stddef.h>
  18. # define STDDEF
  19. #endif
  20.  
  21. typedef unsigned long long ub8;
  22. #define UB8MAXVAL 0xffffffffffffffffLL
  23. #define UB8BITS 64
  24.  
  25. typedef signed long long sb8;
  26. #define SB8MAXVAL 0x7fffffffffffffffLL
  27.  
  28. typedef unsigned long int ub4; /* unsigned 4-byte quantities */
  29. #define UB4MAXVAL 0xffffffff
  30.  
  31. typedef signed long int sb4;
  32. #define UB4BITS 32
  33. #define SB4MAXVAL 0x7fffffff
  34.  
  35. typedef unsigned short int ub2;
  36. #define UB2MAXVAL 0xffff
  37. #define UB2BITS 16
  38.  
  39. typedef signed short int sb2;
  40. #define SB2MAXVAL 0x7fff
  41.  
  42. /* unsigned 1-byte quantities */
  43. typedef unsigned char ub1;
  44. #define UB1MAXVAL 0xff
  45. #define UB1BITS 8
  46.  
  47. /* signed 1-byte quantities */
  48. typedef signed char sb1;
  49. #define SB1MAXVAL 0x7f
  50.  
  51. /* fastest type available */
  52. typedef int word;
  53.  
  54. #define bis(target,mask) ((target) |= (mask))
  55. #define bic(target,mask) ((target) &= ~(mask))
  56. #define bit(target,mask) ((target) & (mask))
  57.  
  58. #ifndef min
  59. # define min(a,b) (((a)<(b)) ? (a) : (b))
  60. #endif /* min */
  61.  
  62. #ifndef max
  63. # define max(a,b) (((a)<(b)) ? (b) : (a))
  64. #endif /* max */
  65.  
  66. #ifndef abs
  67. # define abs(a) (((a)>0) ? (a) : -(a))
  68. #endif
  69.  
  70. #ifndef align
  71. # define align(a) (((ub4)a+(sizeof(void *)-1))&(~(sizeof(void *)-1)))
  72. #endif /* align */
  73.  
  74. #define RAND_TRUE 1
  75. #define RAND_FALSE 0
  76.  
  77. #define RAND_SUCCESS 0 /* 1 on VAX */
  78.  
  79. #endif /* _RANDSTD_H__ */

  1. /**
  2. * rand.h
  3. * definitions for a random number generator
  4. * -----------------------------------------------------------------------------
  5. * By Bob Jenkins, 1996, Public Domain
  6. * MODIFIED:
  7. * 960327: Creation (addition of randinit, really)
  8. * 970719: use context, not global variables, for internal state
  9. * 980324: renamed seed to flag
  10. * 980605: recommend RANDSIZL=4 for noncryptography.
  11. * 010626: note this is public domain
  12. * -----------------------------------------------------------------------------
  13. *
  14. * 2015-01-19: revised by cheungmine
  15. */
  16. #ifndef _RAND_H__
  17. #define _RAND_H__
  18.  
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22.  
  23. #include "randstd.h"
  24.  
  25. #define RANDSIZL (8)
  26. #define RANDSIZ (1<<RANDSIZL)
  27.  
  28. /**
  29. * context of random number generator
  30. */
  31. struct randctx_ub4
  32. {
  33. ub4 randcnt;
  34. ub4 seed[RANDSIZ];
  35. ub4 mm[RANDSIZ];
  36. ub4 aa;
  37. ub4 bb;
  38. ub4 cc;
  39. };
  40. typedef struct randctx_ub4 randctx;
  41.  
  42. /**
  43. * context of random number generator for 64-bits int
  44. */
  45. struct randctx_ub8
  46. {
  47. ub8 randcnt;
  48. ub8 seed[RANDSIZ];
  49. ub8 mm[RANDSIZ];
  50. ub8 aa;
  51. ub8 bb;
  52. ub8 cc;
  53. };
  54. typedef struct randctx_ub8 randctx64;
  55.  
  56. /**
  57. * randinit
  58. * init rand seed
  59. */
  60. extern void rand_init(randctx *r, word time_as_seed);
  61.  
  62. extern void rand64_init(randctx64 *r, word time_as_seed);
  63.  
  64. /**
  65. * rand
  66. * Call rand(randctx *) to retrieve a single 32-bit random value.
  67. */
  68. extern ub4 rand(randctx *r);
  69.  
  70. extern ub4 randint(randctx *r, ub4 rmin, ub4 rmax);
  71.  
  72. extern ub8 rand64(randctx64 *r);
  73.  
  74. extern ub8 randint64(randctx64 *r, ub8 rmin, ub8 rmax);
  75.  
  76. #ifdef __cplusplus
  77. }
  78. #endif
  79.  
  80. #endif /* _RAND_H__ */

  1. /**
  2. * rand.c
  3. * By Bob Jenkins. My random number generator, ISAAC. Public Domain.
  4. * -----------------------------------------------------------------------------
  5. * MODIFIED:
  6. * 960327: Creation (addition of randinit, really)
  7. * 970719: use context, not global variables, for internal state
  8. * 980324: added main (ifdef'ed out), also rearranged randinit()
  9. * 010626: Note that this is public domain
  10. * -----------------------------------------------------------------------------
  11. *
  12. * 2015-01-19: revised by cheungmine
  13. */
  14. #include "rand.h"
  15.  
  16. #include <time.h>
  17.  
  18. /**
  19. *==============================================================================
  20. * 32-bits int random generator
  21. *==============================================================================
  22. */
  23. #define isaac_golden_ratio32 0x9e3779b9
  24. #define ind32(mm,x) (*(ub4 *)((ub1 *)(mm) + ((x) & ((RANDSIZ-1)<<2))))
  25. #define rngstep32(mix,a,b,mm,m,m2,r,x) \
  26. { \
  27. x = *m; \
  28. a = (a^(mix)) + *(m2++); \
  29. *(m++) = y = ind32(mm,x) + a + b; \
  30. *(r++) = b = ind32(mm,y>>RANDSIZL) + x; \
  31. }
  32.  
  33. #define mix32(a,b,c,d,e,f,g,h) \
  34. { \
  35. a^=b<<11; d+=a; b+=c; \
  36. b^=c>>2; e+=b; c+=d; \
  37. c^=d<<8; f+=c; d+=e; \
  38. d^=e>>16; g+=d; e+=f; \
  39. e^=f<<10; h+=e; f+=g; \
  40. f^=g>>4; a+=f; g+=h; \
  41. g^=h<<8; b+=g; h+=a; \
  42. h^=a>>9; c+=h; a+=b; \
  43. }
  44.  
  45. static void isaac32(randctx *ctx)
  46. {
  47. register ub4 a, b, x, y, *m, *mm, *m2, *r, *mend;
  48. mm = ctx->mm;
  49. r = ctx->seed;
  50. a = ctx->aa;
  51. b = ctx->bb + (++ctx->cc);
  52.  
  53. for (m = mm, mend = m2 = m+(RANDSIZ/2); m<mend; ) {
  54. rngstep32( a<<13, a, b, mm, m, m2, r, x);
  55. rngstep32( a>>6 , a, b, mm, m, m2, r, x);
  56. rngstep32( a<<2 , a, b, mm, m, m2, r, x);
  57. rngstep32( a>>16, a, b, mm, m, m2, r, x);
  58. }
  59.  
  60. for (m2 = mm; m2<mend; ) {
  61. rngstep32( a<<13, a, b, mm, m, m2, r, x);
  62. rngstep32( a>>6 , a, b, mm, m, m2, r, x);
  63. rngstep32( a<<2 , a, b, mm, m, m2, r, x);
  64. rngstep32( a>>16, a, b, mm, m, m2, r, x);
  65. }
  66.  
  67. ctx->bb = b;
  68. ctx->aa = a;
  69. }
  70.  
  71. #define gen_rand32(r) \
  72. (!(r)->randcnt-- ? \
  73. (isaac32(r), (r)->randcnt=RANDSIZ-1, (r)->seed[(r)->randcnt]) : \
  74. (r)->seed[(r)->randcnt])
  75.  
  76. void rand_init(randctx *ctx, word time_as_seed)
  77. {
  78. word i;
  79. ub4 a, b, c, d, e, f, g, h;
  80. ub4 *m, *r;
  81. ctx->aa = ctx->bb = ctx->cc = 0;
  82. m = ctx->mm;
  83. r = ctx->seed;
  84. a = b = c = d = e = f = g = h = isaac_golden_ratio32; /* the golden ratio */
  85.  
  86. /* init seed */
  87. for (i=0; i<256; ++i) {
  88. r[i] = (ub4) (time_as_seed? time(0) : 0);
  89. }
  90.  
  91. for (i=0; i<4; ++i) { /* scramble it */
  92. mix32(a,b,c,d,e,f,g,h);
  93. }
  94.  
  95. if (1) {
  96. /* initialize using the contents of r[] as the seed */
  97. for (i=0; i<RANDSIZ; i+=8) {
  98. a+=r[i ]; b+=r[i+1]; c+=r[i+2]; d+=r[i+3];
  99. e+=r[i+4]; f+=r[i+5]; g+=r[i+6]; h+=r[i+7];
  100. mix32(a,b,c,d,e,f,g,h);
  101. m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
  102. m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
  103. }
  104.  
  105. /* do a second pass to make all of the seed affect all of m */
  106. for (i=0; i<RANDSIZ; i+=8) {
  107. a+=m[i ]; b+=m[i+1]; c+=m[i+2]; d+=m[i+3];
  108. e+=m[i+4]; f+=m[i+5]; g+=m[i+6]; h+=m[i+7];
  109. mix32(a,b,c,d,e,f,g,h);
  110. m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
  111. m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
  112. }
  113. } else {
  114. /* Never run to this: fill in m[] with messy stuff */
  115. for (i=0; i<RANDSIZ; i+=8) {
  116. mix32(a,b,c,d,e,f,g,h);
  117. m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
  118. m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
  119. }
  120. }
  121.  
  122. isaac32(ctx); /* fill in the first set of results */
  123. ctx->randcnt = RANDSIZ; /* prepare to use the first set of results */
  124. }
  125.  
  126. /**
  127. * randint
  128. * get 32-bits unsigned integer random
  129. */
  130. ub4 rand(randctx *r)
  131. {
  132. return gen_rand32(r);
  133. }
  134.  
  135. /**
  136. * randint
  137. * get integer random between rmin and rmax
  138. */
  139. ub4 randint(randctx *r, ub4 rmin, ub4 rmax)
  140. {
  141. if (! r->randcnt-- ) {
  142. isaac32(r);
  143. r->randcnt = RANDSIZ - 1;
  144. }
  145.  
  146. ub4 ret = (ub4) r->seed[r->randcnt];
  147.  
  148. return ret % (ub4) (rmax - rmin + 1) + rmin;
  149. }
  150.  
  151. /**
  152. *==============================================================================
  153. * 64 bits int random generator
  154. *==============================================================================
  155. */
  156. #define isaac_golden_ratio64 0x9e3779b97f4a7c13LL
  157. #define ind64(mm,x) (*(ub8 *)((ub1 *)(mm) + ((x) & ((RANDSIZ-1)<<3))))
  158. #define rngstep64(mix,a,b,mm,m,m2,r,x) \
  159. { \
  160. x = *m; \
  161. a = (mix) + *(m2++); \
  162. *(m++) = y = ind64(mm,x) + a + b; \
  163. *(r++) = b = ind64(mm,y>>RANDSIZL) + x; \
  164. }
  165.  
  166. #define mix64(a,b,c,d,e,f,g,h) \
  167. { \
  168. a-=e; f^=h>>9; h+=a; \
  169. b-=f; g^=a<<9; a+=b; \
  170. c-=g; h^=b>>23; b+=c; \
  171. d-=h; a^=c<<15; c+=d; \
  172. e-=a; b^=d>>14; d+=e; \
  173. f-=b; c^=e<<20; e+=f; \
  174. g-=c; d^=f>>17; f+=g; \
  175. h-=d; e^=g<<14; g+=h; \
  176. }
  177.  
  178. static void isaac64(randctx64 *ctx)
  179. {
  180. register ub8 a,b,x,y,*m,*mm,*m2,*r,*mend;
  181. mm = ctx->mm;
  182. r = ctx->seed;
  183. a = ctx->aa;
  184. b = ctx->bb + (++ctx->cc);
  185.  
  186. for (m = mm, mend = m2 = m+(RANDSIZ/2); m<mend; ) {
  187. rngstep64(~(a^(a<<21)), a, b, mm, m, m2, r, x);
  188. rngstep64( a^(a>>5) , a, b, mm, m, m2, r, x);
  189. rngstep64( a^(a<<12) , a, b, mm, m, m2, r, x);
  190. rngstep64( a^(a>>33) , a, b, mm, m, m2, r, x);
  191. }
  192.  
  193. for (m2 = mm; m2<mend; ) {
  194. rngstep64(~(a^(a<<21)), a, b, mm, m, m2, r, x);
  195. rngstep64( a^(a>>5) , a, b, mm, m, m2, r, x);
  196. rngstep64( a^(a<<12) , a, b, mm, m, m2, r, x);
  197. rngstep64( a^(a>>33) , a, b, mm, m, m2, r, x);
  198. }
  199. ctx->bb = b;
  200. ctx->aa = a;
  201. }
  202.  
  203. #define gen_rand64(r) \
  204. (!(r)->randcnt-- ? (isaac64(r), (r)->randcnt=RANDSIZ-1, (r)->seed[(r)->randcnt]) : \
  205. (r)->seed[(r)->randcnt])
  206.  
  207. void rand64_init(randctx64 *ctx, word time_as_seed)
  208. {
  209. word i;
  210. ub8 a,b,c,d,e,f,g,h;
  211. ub8 *mm, *r;
  212. ctx->aa = ctx->bb = ctx->cc = (ub8) 0;
  213.  
  214. a=b=c=d=e=f=g=h= isaac_golden_ratio64; /* the golden ratio */
  215.  
  216. mm = ctx->mm;
  217. r = ctx->seed;
  218.  
  219. /* init seed */
  220. for (i=0; i<256; ++i) {
  221. r[i] = (ub8) (time_as_seed? time(0) : 0);
  222. }
  223.  
  224. for (i=0; i<4; ++i) { /* scramble it */
  225. mix64(a,b,c,d,e,f,g,h);
  226. }
  227.  
  228. for (i=0; i<RANDSIZ; i+=8) { /* fill in mm[] with messy stuff */
  229. if (1) { /* use all the information in the seed */
  230. a+=r[i ]; b+=r[i+1]; c+=r[i+2]; d+=r[i+3];
  231. e+=r[i+4]; f+=r[i+5]; g+=r[i+6]; h+=r[i+7];
  232. }
  233. mix64(a,b,c,d,e,f,g,h);
  234. mm[i ]=a; mm[i+1]=b; mm[i+2]=c; mm[i+3]=d;
  235. mm[i+4]=e; mm[i+5]=f; mm[i+6]=g; mm[i+7]=h;
  236. }
  237.  
  238. if (1) {
  239. /* do a second pass to make all of the seed affect all of mm */
  240. for (i=0; i<RANDSIZ; i+=8) {
  241. a+=mm[i ]; b+=mm[i+1]; c+=mm[i+2]; d+=mm[i+3];
  242. e+=mm[i+4]; f+=mm[i+5]; g+=mm[i+6]; h+=mm[i+7];
  243. mix64(a,b,c,d,e,f,g,h);
  244. mm[i ]=a; mm[i+1]=b; mm[i+2]=c; mm[i+3]=d;
  245. mm[i+4]=e; mm[i+5]=f; mm[i+6]=g; mm[i+7]=h;
  246. }
  247. }
  248.  
  249. isaac64(ctx); /* fill in the first set of results */
  250. ctx->randcnt = RANDSIZ; /* prepare to use the first set of results */
  251. }
  252.  
  253. /**
  254. * rand64
  255. * get 64-bits unsigned integer random
  256. */
  257. ub8 rand64(randctx64 *r)
  258. {
  259. return (ub8) (gen_rand64(r));
  260. }
  261.  
  262. /**
  263. * randint64
  264. * get 64-bits unsigned integer random
  265. */
  266. ub8 randint64(randctx64 *r, ub8 rmin, ub8 rmax)
  267. {
  268. if (! r->randcnt-- ) {
  269. isaac64(r);
  270. r->randcnt = RANDSIZ - 1;
  271. }
  272.  
  273. ub8 ret = (ub8) r->seed[r->randcnt];
  274.  
  275. return ret % (ub8) (rmax - rmin + 1) + rmin;
  276. }
  277.  
  278. #ifdef NEVER
  279. static void inner_test32()
  280. {
  281. ub4 i,j;
  282. randctx ctx;
  283. rand_init(&ctx, RAND_FALSE);
  284.  
  285. for (i=0; i<2; ++i) {
  286. isaac32(&ctx);
  287.  
  288. for (j=0; j<256; ++j) {
  289. printf("%.8lx",ctx.seed[j]);
  290. if ((j&7)==7) {
  291. printf("\n");
  292. }
  293. }
  294. }
  295. }
  296.  
  297. static void inner_test64()
  298. {
  299. ub8 i,j;
  300. randctx64 ctx;
  301. rand64_init(&ctx, RAND_FALSE);
  302.  
  303. for (i=0; i<2; ++i) {
  304. isaac64(&ctx);
  305.  
  306. for (j=0; j<RANDSIZ; ++j) {
  307. printf("%.8lx%.8lx",(ub4)(ctx.seed[j]>>32),(ub4)ctx.seed[j]);
  308.  
  309. if ((j&3)==3) {
  310. printf("\n");
  311. }
  312. }
  313. }
  314. }
  315.  
  316. static void usage()
  317. {
  318. int i;
  319.  
  320. randctx ctx;
  321. randctx64 ctx64;
  322.  
  323. rand_init(&ctx, RAND_TRUE);
  324.  
  325. rand64_init(&ctx64, RAND_TRUE);
  326.  
  327. for (i=0; i<100; ++i) {
  328. printf("%03d: %d\n", i, (sb4)randint(&ctx, -100, 100));
  329. printf("%03d: %lld\n", i, (sb8)randint64(&ctx64, -100, 100));
  330. }
  331. }
  332.  
  333. int main()
  334. {
  335. inner_test32();
  336. inner_test64();
  337.  
  338. usage();
  339. }
  340.  
  341. #endif

C语言生成32位和64位随机数算法的更多相关文章

  1. 64位主机64位oracle下装32位客户端ODAC(NFPACS版)

    64位主机64位oracle下装32位客户端ODAC(NFPACS版) by dd 1.下载Oracle Data Access Components(ODAC) Xcopy的两个版本: x86:(我 ...

  2. Linux系统查看系统是32位还是64位方法总结

    这篇博客是总结.归纳查看Linux系统是32位还是64位的一些方法,很多内容来自网上网友的博客.本篇只是整理.梳理这方面的知识,方便自己忘记的时候随时查看. 方法1:getconf LONG_BIT ...

  3. oracle 32位导64位

    oracle 32位导64位 SHUTDOWN IMMEDIATE; STARTUP MOUNT; ALTER SYSTEM ENABLE RESTRICTED SESSION; ; ; ALTER ...

  4. Shell脚本中,如何判断Linux系统是32位还是64位?

    一行就能搞定,输出32或者64 可以用“和. 参考代码如下: ldconfig if [ $(getconf WORD_BIT) = '32' ] && [ $(getconf LON ...

  5. 【转】Tomcat版本是32位、64位问题

    转载地址:http://www.cnblogs.com/greensleeves/p/3168541.html 最近遇到一个Tomcat windows安装版本是32位还是64位问题.由于一系列原因, ...

  6. 查看Linux是32位还是64位

    最直接简洁的办法: 在linux终端输入getconf LONG_BIT命令 如果是32位机器,则结果为32 [root@localhost ~]# getconf LONG_BIT 32 如果是64 ...

  7. Windows2003 IIS6.0支持32位和64位两种模式的设置方法

    IIS 6.0 可支持 32 位和 64 位两种模式.但是,IIS 6.0 不支持在 64 位版本的 Windows 上同时运行这两种模式.ASP.NET 1.1 只在 32 位模式下运行.而 ASP ...

  8. linux-查看系统是32位还是64位

    可以用命令“getconf LONG_BIT”查看, 如果返回的结果是32则说明是32位,返回的结果是64则说明是64位. 此外还可以使用命令“uname -a”查看, 输出的结果中,如果有x86_6 ...

  9. 查看linux机器是32位还是64位的方法

    file /sbin/init 或者 file /bin/ls/sbin/init: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dyna ...

随机推荐

  1. 在做自动化测试之前你需要知道的,转自:http://www.cnblogs.com/fnng/p/3653793.html

    什么是自动化测? 做测试好几年了,真正学习和实践自动化测试一年,自我感觉这一个年中收获许多.一直想动笔写一篇文章分享自动化测试实践中的一些经验.终于决定花点时间来做这件事儿. 首先理清自动化测试的概念 ...

  2. Unsupported major.minor version 52.0

    今天运行项目,切换一下eclipse,运行程序突然发现普通的类main()方法无法运行,报错详细信息如下: Exception in thread "main" java.lang ...

  3. 使用Apache的ab进行压力测试

    概述 ab是apache自带的压力测试工具,当安装完apache的时候,就可以在bin下面找到ab然后进行apache 负载压力测试. 后台测试开发中,常用的压力测试服务,php一般选择xampp,下 ...

  4. 视频编码器评测系统:VideoCodecRank

    视频编码器领域一直有个比较复杂的问题:mpeg2.divx.xvid.mpeg4.vp8.vp9.x264.openh264.x265等等这一系列编码器到底哪个好?而对于同一种视频编码器,又包括了各种 ...

  5. 谷歌面试题:输入是两个整数数组,他们任意两个数的和又可以组成一个数组,求这个和中前k个数怎么做?

    谷歌面试题:输入是两个整数数组,他们任意两个数的和又可以组成一个数组,求这个和中前k个数怎么做? 分析: "假设两个整数数组为A和B,各有N个元素,任意两个数的和组成的数组C有N^2个元素. ...

  6. Objc运行时读取和写入plist文件遇到的问题

    下面是本猫保持游戏NPC和物件交互的plist文件: 随着游戏和玩家逐步发生互动,玩家会修改人物和物件的交互的状态.这也是RPG游戏最基本的功能. 在切换每个地图时需要将上一个地图发生的改变存储到pl ...

  7. 关于bootstrap-fileinput

    最近搞了一个很简单的项目,里面需要文件上传.当然文件上传也是很简单的,不过做出成品之后发现,卧槽,火狐和谷歌两个浏览器显示的内容不一致. 如下图,左边的是谷歌显示,右边是火狐显示. 其实,作为一个后台 ...

  8. Android之Gallery和Spinner-Android学习之旅(二十九)

    Spinner简介 spinner是竖直方向展开一个列表供选择.和gallery都是继承了AbsSpinner,AbsSpinner继承了AdapterView,因此AdaptyerView的属性都可 ...

  9. 5.创建表,使用alter进行表信息的增删改,Oracle回收站,集合运算

     1  Oracle基于用户的管理方案 2 DDL语句可以管理数据库的对象有:视图   索引  序列  同义词   约束 3  创建一个表,有2个条件(1 有权限:2有表空间) Oracle给你提 ...

  10. HMAC

    Hash-based Message Authentication Code HMAC是IP安全里必须实现的MAC方案,并且其他Internet协议中(如SSL)也使用了HMAC.HMAC已作为NIS ...