产生随机数

tangcx 2003-12-03 11:07:11
产生随机数的算法是怎么样的???
...全文
374 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangminjie20053387 2010-06-21
  • 打赏
  • 举报
回复
8楼的 顶一下
我是只虫 2003-12-05
  • 打赏
  • 举报
回复
提醒一句,自己编算法一定得注意随机数的
分布均匀性

相邻数的相关性。
lzumcj_pa18 2003-12-05
  • 打赏
  • 举报
回复
///////////////////////////////////////////////////////////////
// Module: CRand.h
// Purpose: 随机数类头文件
// Author: lzumcj
// Date: 2003.10.30
///////////////////////////////////////////////////////////////

class CRand
{
public:
CRand( unsigned seed = time(0) ) ;
#include <cstdlib> // for srand(),rand()
#include <ctime> // for time()

int getRandom( int min , int max ) ;
};

///////////////////////////////////////////////////////////////
// Module: CRand.cpp
// Purpose: 随机数类实现
// Author: lzumcj
// Date: 2003.10.30
///////////////////////////////////////////////////////////////

#include "CRand.h"

class CRand
{
public:
CRand( unsigned seed = time(0) ) ;
int getRandom( int min , int max ) ;
};

CRand::CRand( unsigned seed )
{
srand( seed ) ;
}

int CRand::getRandom( int min , int max )
{
if ( max < 0 )
max = 0 ;
if ( ( min == max ) && 0 == min )
return 0 ;
return rand() % ( max + 1 - min ) + min ;
}

///////////////////////////////////////////////////////////////
// Module: random.cpp
// Purpose: 测试随机数类
// Author: lzumcj
// Date: 2003.10.30
///////////////////////////////////////////////////////////////

#include <iostream>
#include "CRand.h"

int main()
{
CRand rand ;

for ( int i = 0 ; i < 10 ; i++ )
std::cout << " Random number " << i + 1 << " : "
<< rand.getRandom( 0 , 100 ) << std::endl ;

return 0 ;
}
aojunpeng313001 2003-12-05
  • 打赏
  • 举报
回复
狗哥的写的东西就是好,不过除/* * */没看见别的了,呵呵(小弟看的时候还在分析*的东西,以为是语句呢!!)
021850524 2003-12-05
  • 打赏
  • 举报
回复
直接用不就得了吗
先srand初始化随机数种子,后rand生成随机数.
zpengenpz 2003-12-04
  • 打赏
  • 举报
回复
收藏一下!
transformers 2003-12-04
  • 打赏
  • 举报
回复
学习一下!
fireseed 2003-12-04
  • 打赏
  • 举报
回复
/***
*rand.c - random number generator
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines rand(), srand() - random number generator
*
*******************************************************************************/

#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>
#include <stdlib.h>

#ifndef _MT
static long holdrand = 1L;
#endif /* _MT */

/***
*void srand(seed) - seed the random number generator
*
*Purpose:
* Seeds the random number generator with the int given. Adapted from the
* BASIC random number generator.
*
*Entry:
* unsigned seed - seed to seed rand # generator with
*
*Exit:
* None.
*
*Exceptions:
*
*******************************************************************************/

void __cdecl srand (
unsigned int seed
)
{
#ifdef _MT

_getptd()->_holdrand = (unsigned long)seed;

#else /* _MT */
holdrand = (long)seed;
#endif /* _MT */
}


/***
*int rand() - returns a random number
*
*Purpose:
* returns a pseudo-random number 0 through 32767.
*
*Entry:
* None.
*
*Exit:
* Returns a pseudo-random number 0 through 32767.
*
*Exceptions:
*
*******************************************************************************/

int __cdecl rand (
void
)
{
#ifdef _MT

_ptiddata ptd = _getptd();

return( ((ptd->_holdrand = ptd->_holdrand * 214013L
+ 2531011L) >> 16) & 0x7fff );

#else /* _MT */
return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
#endif /* _MT */
}
lipeanut 2003-12-03
  • 打赏
  • 举报
回复
没问题的话记住给分亚!!
/*1.从同一个种子开始*/
#include <stdio.h>
#include <conio.h>
static unsigned long int next=1;

int rand0(void)
{
next=next*1103515245+12345;
return (unsigned int)(next/65536)%32768;
}

int main(void)
{
int count;

for(count=0;count<5;count++)
printf("%hd\n",rand0());
getch();
return 0;
}

/*2.重置种子*/
#include <stdio.h>
#include <conio.h>
static unsigned long int next=1;

int rand1(void)
{
next=next*1103515245+12345;
return (unsigned int)(next/65536)%32768;
}

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

int main(void)
{
int count;
unsigned int seed;

printf("please input seed:");
scanf("%u",&seed);
srand1(seed);
for(count=0;count<5;count++)
printf("%hd\n",rand1());
getch();
return 0;
}

/*3.利用利用时钟产生种子
ANSI C程序库提供了rand()函数来产生随机数;
ANSI C程序库提供了srand()函数来产生种子;
ANSI C程序库提供了time()函数返回系统时间。
*/
#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <stdlib.h>

int main(void)

{
int i;
time_t t;
clrscr();
t = time(NULL);
srand((unsigned) t);
for(i=0; i<10; i++) printf("%d\n", rand()%10);
getch();
return 0;
}
smalltalk 2003-12-03
  • 打赏
  • 举报
回复
很复杂,建议你研究一下C标准库的代码。

但也可以很简单。向空中抛银币就可以得到一个随机数。 :)

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧