找出什么是随机数生成器在C ++中种子 [英] Find out what a random number generator was seeded with in C++

查看:123
本文介绍了找出什么是随机数生成器在C ++中种子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非托管的c ++控制台应用程序,其中我使用srand()和rand()。我不需要这个来解决一个特定的问题,但很好奇:是原始的种子传递到srand()存储在内存中的某处,我可以查询?有什么方法可以弄明白种子是什么?

I've got an unmanaged c++ console application in which I'm using srand() and rand(). I don't need this to solve a particular problem, but was curious: is the original seed passed to srand() stored somewhere in memory that I can query? Is there any way to figure out what the seed was?

推荐答案

种子不需要存储,返回的数字为

The seed is not required to be stored, only the last random number returned is.

以下是联机帮助页中的示例:

Here's the example from the manpage:

       static unsigned long next = 1;

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

       void mysrand(unsigned seed) {
           next = seed;
       }

这篇关于找出什么是随机数生成器在C ++中种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆