• <legend id='vLz9B'><style id='vLz9B'><dir id='vLz9B'><q id='vLz9B'></q></dir></style></legend>

      <bdo id='vLz9B'></bdo><ul id='vLz9B'></ul>
    <tfoot id='vLz9B'></tfoot>
  • <i id='vLz9B'><tr id='vLz9B'><dt id='vLz9B'><q id='vLz9B'><span id='vLz9B'><b id='vLz9B'><form id='vLz9B'><ins id='vLz9B'></ins><ul id='vLz9B'></ul><sub id='vLz9B'></sub></form><legend id='vLz9B'></legend><bdo id='vLz9B'><pre id='vLz9B'><center id='vLz9B'></center></pre></bdo></b><th id='vLz9B'></th></span></q></dt></tr></i><div id='vLz9B'><tfoot id='vLz9B'></tfoot><dl id='vLz9B'><fieldset id='vLz9B'></fieldset></dl></div>
    1. <small id='vLz9B'></small><noframes id='vLz9B'>

        特殊的简单随机数发生器

        时间:2023-08-03

          <legend id='FIpNJ'><style id='FIpNJ'><dir id='FIpNJ'><q id='FIpNJ'></q></dir></style></legend>
          <tfoot id='FIpNJ'></tfoot>
            <tbody id='FIpNJ'></tbody>
          • <small id='FIpNJ'></small><noframes id='FIpNJ'>

              <bdo id='FIpNJ'></bdo><ul id='FIpNJ'></ul>

                1. <i id='FIpNJ'><tr id='FIpNJ'><dt id='FIpNJ'><q id='FIpNJ'><span id='FIpNJ'><b id='FIpNJ'><form id='FIpNJ'><ins id='FIpNJ'></ins><ul id='FIpNJ'></ul><sub id='FIpNJ'></sub></form><legend id='FIpNJ'></legend><bdo id='FIpNJ'><pre id='FIpNJ'><center id='FIpNJ'></center></pre></bdo></b><th id='FIpNJ'></th></span></q></dt></tr></i><div id='FIpNJ'><tfoot id='FIpNJ'></tfoot><dl id='FIpNJ'><fieldset id='FIpNJ'></fieldset></dl></div>
                2. 本文介绍了特殊的简单随机数发生器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何创建一个函数,每次调用都会生成一个随机整数?这个数字必须尽可能随机(根据均匀分布).只允许使用一个静态变量,最多 3 个基本步骤,其中每个步骤仅包含 arity 1 或 2.

                  How to create a function, which on every call generates a random integer number? This number must be most random as possible (according to uniform distribution). It is only allowed to use one static variable and at most 3 elementary steps, where each step consists of only one basic arithmetic operation of arity 1 or 2.

                  示例:

                  int myrandom(void){
                    static int x;
                    x = some_step1;
                    x = some_step2;
                    x = some_step3;
                    return x;
                  }
                  

                  基本算术运算有+、-、%、and、not、xor、or、左移、右移、乘除.当然,不允许使用 rand()、random() 或类似的东西.

                  Basic arithmetic operations are +,-,%,and, not, xor, or, left shift, right shift, multiplication and division. Of course, no rand(), random() or similar stuff is allowed.

                  推荐答案

                  线性同余生成器是最古老、最简单的方法之一:

                  Linear congruential generators are one of the oldest and simplest methods:

                  int seed = 123456789;
                  
                  int rand()
                  {
                    seed = (a * seed + c) % m;
                    return seed;
                  }
                  

                  只需要一些基本算术运算的指令,这就是你所需要的.

                  Only a few instruction with basic arithmetic operations, that's all you need.

                  请注意,只有在以特定方式选择 acm 时,此算法才能正常工作!

                  Mind that this algorithm works fine only if a, c and m are chosen in a particular way!

                  为了保证这个序列的最长可能周期,cm 应该是互质的,a-1 应该可以被所有质数整除m 的因数,如果 m 可被 4 整除,则为 4.

                  To guarantee the longest possible period of this sequence, c and m should be coprime, a − 1 should be divisible by all prime factors of m, and also for 4 if m is divisible by 4.

                  维基百科上显示了一些参数示例:例如某些编译器的ANSI C建议 m = 2³¹、a = 1103515245 和 c = 12345.

                  Some examples of parameters are shown on Wikipedia: for example ANSI C for some compilers proposes m = 2 ³¹, a = 1103515245 and c = 12345.

                  这篇关于特殊的简单随机数发生器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:素性测试的最快算法 下一篇:什么是半开范围和离终值

                  相关文章

                  最新文章

                3. <i id='RmErE'><tr id='RmErE'><dt id='RmErE'><q id='RmErE'><span id='RmErE'><b id='RmErE'><form id='RmErE'><ins id='RmErE'></ins><ul id='RmErE'></ul><sub id='RmErE'></sub></form><legend id='RmErE'></legend><bdo id='RmErE'><pre id='RmErE'><center id='RmErE'></center></pre></bdo></b><th id='RmErE'></th></span></q></dt></tr></i><div id='RmErE'><tfoot id='RmErE'></tfoot><dl id='RmErE'><fieldset id='RmErE'></fieldset></dl></div>

                  <small id='RmErE'></small><noframes id='RmErE'>

                4. <tfoot id='RmErE'></tfoot>

                    1. <legend id='RmErE'><style id='RmErE'><dir id='RmErE'><q id='RmErE'></q></dir></style></legend>
                        <bdo id='RmErE'></bdo><ul id='RmErE'></ul>