亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
12下一頁
最近訪問板塊 發(fā)新帖
查看: 4060 | 回復(fù): 16
打印 上一主題 下一主題

請問c語言里隨機數(shù)函數(shù)的實現(xiàn)代碼該在哪兒找? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2009-05-05 20:37 |只看該作者 |倒序瀏覽
srand()、rand()的實現(xiàn)代碼查得到嗎?

論壇徽章:
5
2015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:53:172015亞冠之水原三星
日期:2015-06-02 16:34:202015年亞冠紀(jì)念徽章
日期:2015-10-19 18:13:37程序設(shè)計版塊每日發(fā)帖之星
日期:2015-11-08 06:20:00
2 [報告]
發(fā)表于 2009-05-05 20:39 |只看該作者

  1. /***
  2. *rand.c - random number generator
  3. *
  4. *       Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines rand(), srand() - random number generator
  8. *
  9. *******************************************************************************/
  10. #include <cruntime.h>
  11. #include <mtdll.h>
  12. #include <stddef.h>
  13. #include <stdlib.h>
  14. #ifndef _MT
  15. static long holdrand = 1L;
  16. #endif  /* _MT */
  17. /***
  18. *void srand(seed) - seed the random number generator
  19. *
  20. *Purpose:
  21. *       Seeds the random number generator with the int given.  Adapted from the
  22. *       BASIC random number generator.
  23. *
  24. *Entry:
  25. *       unsigned seed - seed to seed rand # generator with
  26. *
  27. *Exit:
  28. *       None.
  29. *
  30. *Exceptions:
  31. *
  32. *******************************************************************************/
  33. void __cdecl srand (
  34.         unsigned int seed
  35.         )
  36. {
  37. #ifdef _MT
  38.         _getptd()->_holdrand = (unsigned long)seed;
  39. #else  /* _MT */
  40.         holdrand = (long)seed;
  41. #endif  /* _MT */
  42. }

  43. /***
  44. *int rand() - returns a random number
  45. *
  46. *Purpose:
  47. *       returns a pseudo-random number 0 through 32767.
  48. *
  49. *Entry:
  50. *       None.
  51. *
  52. *Exit:
  53. *       Returns a pseudo-random number 0 through 32767.
  54. *
  55. *Exceptions:
  56. *
  57. *******************************************************************************/
  58. int __cdecl rand (
  59.         void
  60.         )
  61. {
  62. #ifdef _MT
  63.         _ptiddata ptd = _getptd();
  64.         return( ((ptd->_holdrand = ptd->_holdrand * 214013L
  65.             + 2531011L) >> 16) & 0x7fff );
  66. #else  /* _MT */
  67.         return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
  68. #endif  /* _MT */
  69. }


復(fù)制代碼

論壇徽章:
0
3 [報告]
發(fā)表于 2009-05-05 20:41 |只看該作者
在ISO/IEC 9899:1999 (E)里查到這些內(nèi)容:

EXAMPLE  The following functions define a portable implementation of rand and srand.

static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}

void srand(unsigned int seed)
{
next = seed;
}

這些代碼是在哪個文件里?
還請高人指點。

論壇徽章:
0
4 [報告]
發(fā)表于 2009-05-05 20:42 |只看該作者
這仍然依賴于實現(xiàn)。你用哪個 libc?

論壇徽章:
0
5 [報告]
發(fā)表于 2009-05-05 22:30 |只看該作者

回復(fù) #2 xinglp 的帖子

3Q

論壇徽章:
0
6 [報告]
發(fā)表于 2009-05-05 22:43 |只看該作者

回復(fù) #3 glasslion 的帖子

暈 原來隨機數(shù)就是這樣簡單的運算結(jié)果- -

我還一直以為是啥高級公式算出來的

論壇徽章:
0
7 [報告]
發(fā)表于 2009-05-05 22:57 |只看該作者

論壇徽章:
0
8 [報告]
發(fā)表于 2009-05-06 12:04 |只看該作者
注釋里都說了,這是產(chǎn)生pseudo-random 數(shù)字的方法。
真正隨機數(shù)的產(chǎn)生方法,可以看看內(nèi)核實現(xiàn)。

論壇徽章:
0
9 [報告]
發(fā)表于 2009-05-06 12:09 |只看該作者
二樓的代碼搞定了問題

論壇徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辭舊歲徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
10 [報告]
發(fā)表于 2009-05-06 12:24 |只看該作者
不知道有沒有不依賴與系統(tǒng)時間的隨機數(shù)
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP