OneWayOrAnother 2015-07-25 17:45 采纳率: 30%
浏览 1539

[请教] 关于ANSIC [可移植]伪随机函数rand() 的问题。。。

/* rand0.c ---产生随机数*/
/*使用ANSI C 可移植算法*/
static unsigned long int next=1; //种子
int rand0(void)
{
next=next*1103515245+123456;
return (unsigned int)(next/65536)%32768;
}
void srand0(unsigned int seed)
{
next=seed;
}
/*r_drive0.c 测试rand0() srand0()函数*/
#include
#include

extern void srand0(unsigned int);
extern int rand0(void);
int main(void)
{
int count;
srand0((unsigned int)time(0));
for(count=0;count printf("%d\n",rand0());
return 0;
}
/////////////////////////////////////////////////////////////////////////////////
为什么rand0() 函数里面的那2个公式是可移植算法?

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-07-25 21:25
    关注

    可移植的含义就是完全依靠数学公式,而不依靠特定的计算机和系统,比如线性同余,它产生的伪随机数序列是确定的

    http://www.cnblogs.com/xkfz007/archive/2012/03/27/2420154.html

    什么叫做不可移植?比如说cpu有个硬件指令产生随机数,并且没有软件可以模拟。或者从温度传感器获得当前气温和湿度作为随机数,这些就不能移植。

    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制