如何获得 rand() (C++) 的源代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18969783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:22:46  来源:igfitidea点击:

How can I get the sourcecode for rand() (C++)?

c++random

提问by BBedit

I'm new to programming.

我是编程新手。

I want to know exactly what rand() does.

我想确切地知道 rand() 是做什么的。

Searching only yields examples on its usage. But none explain each step of how the function generates a random number. They treat rand() as a blackbox.

搜索仅产生有关其用法的示例。但是没有人解释函数如何生成随机数的每一步。他们将 rand() 视为黑盒。

I want to know what rand() is doing; each step.

我想知道 rand() 在做什么;每一步。

Is there a resource that will allow me to see exactly what rand() does?This is all open source stuff isn't it? I'll settle for the disassembly, if there's no source.

是否有资源可以让我确切地看到 rand() 的作用?这都是开源的东西不是吗?如果没有来源,我会满足于拆卸。

I know it returns a random number, but how does it generate that number? I want to see each step.

我知道它返回一个随机数,但它是如何生成该数字的?我想看到每一步。

Thank you.

谢谢你。

回答by Carl Norum

Here is the current glibc implementation:

这是当前的 glibc 实现

/* Return a random integer between 0 and RAND_MAX.  */
int
rand (void)
{
  return (int) __random ();
}

That's not much help, but __randomeventually calls __random_r:

这没有多大帮助,但__random最终调用__random_r

/* If we are using the trivial TYPE_0 R.N.G., just do the old linear
   congruential bit.  Otherwise, we do our fancy trinomial stuff, which is the
   same in all the other cases due to all the global variables that have been
   set up.  The basic operation is to add the number at the rear pointer into
   the one at the front pointer.  Then both pointers are advanced to the next
   location cyclically in the table.  The value returned is the sum generated,
   reduced to 31 bits by throwing away the "least random" low bit.
   Note: The code takes advantage of the fact that both the front and
   rear pointers can't wrap on the same call by not testing the rear
   pointer if the front one has wrapped.  Returns a 31-bit random number.  */

int
__random_r (buf, result)
     struct random_data *buf;
     int32_t *result;
{
  int32_t *state;

  if (buf == NULL || result == NULL)
    goto fail;

  state = buf->state;

  if (buf->rand_type == TYPE_0)
    {
      int32_t val = state[0];
      val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
      state[0] = val;
      *result = val;
    }
  else
    {
      int32_t *fptr = buf->fptr;
      int32_t *rptr = buf->rptr;
      int32_t *end_ptr = buf->end_ptr;
      int32_t val;

      val = *fptr += *rptr;
      /* Chucking least random bit.  */
      *result = (val >> 1) & 0x7fffffff;
      ++fptr;
      if (fptr >= end_ptr)
    {
      fptr = state;
      ++rptr;
    }
      else
    {
      ++rptr;
      if (rptr >= end_ptr)
        rptr = state;
    }
      buf->fptr = fptr;
      buf->rptr = rptr;
    }
  return 0;

 fail:
  __set_errno (EINVAL);
  return -1;
}

回答by Johan Henriksson

You can browse the source code for different implementations of the C standard.

您可以浏览 C 标准的不同实现的源代码。

The question has been answered before, you might find what you're looking for at What common algorithms are used for C's rand()?

之前已经回答了这个问题,您可能会在C 的 rand() 使用哪些常用算法?

That answer provides code for glibc's implementation of rand()

该答案为 glibc 的 rand() 实现提供了代码

回答by AndASM

Well, I believe rand is from the C standard library, not the C++ standard library. There is no one implementation of either library, there are several.

好吧,我相信 rand 来自 C 标准库,而不是 C++ 标准库。任何一个库都没有一个实现,有几个。

You could go somewhere like this pageto view the source code for glibc, the c library used on most Linux distributions. For glibc you'd find it in source files under stdlib such as rand.cand random.c.

你可以去类似这个页面的地方查看 glibc 的源代码,这是大多数 Linux 发行版上使用的 c 库。对于 glibc,您可以在 stdlib 下的源文件中找到它,例如rand.crandom.c

A different implementation, such as uClibc might be easier to read. Try hereunder the libc/stdlib folder.

不同的实现,例如 uClibc 可能更容易阅读。在 libc/stdlib 文件夹下试试这里

回答by Zeeshan

I guess, THISis what you are looking for. It contains the detailed explanation of random function, and simple C program to understand the algo.

我想,就是你要找的。它包含随机函数的详细解释,以及理解算法的简单 C 程序。

Edit:

编辑:

You should check THISas well. A possible duplicate.

您应该检查为好。一个可能的重复。

回答by Kevin A. Naudé

The simplest reasonably good pseudo-random number generators are Linear Congruential Generators (LCGs). These are iterations of a formula such as

最简单的相当好的伪随机数生成器是线性同余生成器 (LCG)。这些是公式的迭代,例如

X_{n+1} = (a * X_n  +  c) modulo m

The constants a, c, and m are chosen to given unpredictable sequences. X_0 is the random seed value. Many other algorithms exists, but this is probably enough to get you going.

选择常数 a、c 和 m 以给出不可预测的序列。X_0 是随机种子值。存在许多其他算法,但这可能足以让您继续前进。

Really good pseudo-random number generators are more complex, such as the Mersenne Twister.

真正好的伪随机数生成器更复杂,例如Mersenne Twister