随机数生成器基于当前时间和出生日期

Random Numbers Generator based on Current Time and Birth Date

本文关键字:出生日期 时间 于当前 随机数生成器      更新时间:2023-09-26

我开始学习JavaScript,我发现了这篇文章:http://michalbe.blogspot.ro/2011/02/javascript-random-numbers-with-custom.html

我喜欢自定义种子数生成器的想法,但我对Thor的热爱无法弄清楚,我真的需要一个用纯javascript或像jQuery这样的库完成的实际示例,如果它更容易。

开始:

所以我想要生成10个不同的数字,用"-"(减号)分隔,每个数字的范围是1-50,我想要每个数字的种子由两项相乘组成:

  1. 以尽可能大的数字序列显示当前时间(每个数字延迟1秒,因此时间将不同)
  2. 您的出生日期(从3个选择输入)

[!我还想知道我们如何以不同的方式动画生成这10个数字,比如……例如旧火车站进站/出站机械显示的动画,或者不同的HTML5画布粒子技术或CSS3等等。-如果你认为太多,可以在另一个问题中做。

如果你能帮我解决这个问题,我将永远感激不尽!

谢谢!

var CustomRandom = function(nseed) {    
  
  var seed,    
    constant = Math.pow(2, 13)+1,    
    prime = 1987,    
//any prime number, needed for calculations, 1987 is my favorite:)  
    maximum = 50;    
//maximum number needed for calculation the float precision of the numbers (10^n where n is number of digits after dot)  
    if (nseed) {    
      seed = nseed;    
    }    
    
    if (seed == null) {    
//before you will correct me in this comparison, read Andrea Giammarchi's text about coercion http://goo.gl/N4jCB  
      
      seed = (new Date()).getTime();   
//if there is no seed, use timestamp     
    }     
    
    return {    
      next : function(min, max) {    
        seed *= constant;    
        seed += prime;    
           
      
        return min && max ? min+seed%maximum/maximum*(max-min) : seed%maximum/maximum;  
// if 'min' and 'max' are not provided, return random number between 0 & 1  
      }    
    }    
}
var rng = CustomRandom(09031887);
//use '09031887' as a seed ?
    rng.next();
    rng.next();
});
<b>Your birth date:</b><br>
Day: 
<select id="day">
<option selected="selected">01</option>
<option>02</option><option>03</option><option>04</option><option>05</option><option>06</option><option>07</option><option>08</option><option>09</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option><option>16</option><option>17</option><option>18</option><option>19</option><option>20</option><option>21</option><option>22</option><option>23</option><option>24</option><option>25</option><option>26</option><option>27</option><option>28</option><option>29</option><option>30</option><option>31</option>
</select>
Month:
<select id="month">
<option selected="selected">01</option>
<option>02</option><option>03</option><option>04</option><option>05</option><option>06</option><option>07</option><option>08</option><option>09</option><option>10</option><option>11</option><option>12</option>
</select>
Year: 
<select id="year">
<option selected="selected">1998</option>
<option>1997</option><option>1996</option><option>1995</option><option>1994</option><option>1993</option><option>1992</option><option>1991</option><option>1990</option><option>1989</option><option>1988</option><option>1987</option><option>1986</option><option>1985</option><option>1984</option><option>1983</option><option>1982</option><option>1981</option><option>1980</option><option>1979</option><option>1978</option><option>1977</option><option>1976</option><option>1975</option><option>1976</option><option>1975</option><option>1976</option>
</select>
<br><br>
<span id="nr1"></span> - 
<span id="nr2"></span> - 
<span id="nr3"></span> - 
<span id="nr4"></span> - 
<span id="nr5"></span> - 
<span id="nr6"></span> - 
<span id="nr7"></span> - 
<span id="nr8"></span> - 
<span id="nr9"></span> - 
<span id="nr10"></span>
<br><br>
<button>Generate</button>

如果您在提供的代码中修复了SyntaxError,那么Javascript代码实际上可以工作。你没有任何事件处理程序来控制它与你提供的HTML。

var CustomRandom = function (nseed) {
    var seed,
        constant = Math.pow(2, 13) + 1,
        prime = 1987,
        //any prime number, needed for calculations, 1987 is my favorite:)  
        maximum = 50;
        //maximum number needed for calculation the float precision of the numbers (10^n where n is number of digits after dot)  
    
    if (nseed) {
        seed = nseed;
    }
    if (seed == null) {
        //before you will correct me in this comparison, read Andrea Giammarchi's text about coercion http://goo.gl/N4jCB  
        seed = (new Date()).getTime();
        //if there is no seed, use timestamp     
    }
    return {
        next: function (min, max) {
            seed *= constant;
            seed += prime;
            return min && max ? min + seed % maximum / maximum * (max - min) : seed % maximum / maximum;
            // if 'min' and 'max' are not provided, return random number between 0 & 1  
        }
    }
}
var rng = CustomRandom(09031887),
    //use '09031887' as a seed ?
    pre = document.getElementById('out');
pre.textContent += rng.next() + ''n';
pre.textContent += rng.next();
<pre id="out"></pre>

使用链接到的库的进一步示例。

Math.seedrandom(09031887);
//use '09031887' as a seed ?
var pre = document.getElementById('out');
pre.textContent += Math.random() + ''n'; // output always 0.21968861683194144
pre.textContent += Math.random(); // output always 0.9079655673042485
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.4.0/seedrandom.min.js"></script>
<pre id="out"></pre>

这是我使用David Bau的脚本(seedrrandom .js)得到的最接近的:

<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.4.0/seedrandom.min.js">
</script>
<b>Your birth date:</b><br>
Day: 
<select id="day">
<option selected="selected">day</option><option value="1">01</option><option value="2">02</option><option value="3">03</option><option value="4">04</option><option value="5">05</option><option value="6">06</option><option value="7">07</option><option value="8">08</option><option value="9">09</option><option>10</option><option>11</option><option>12</option><option>13</option><option>14</option><option>15</option><option>16</option><option>17</option><option>18</option><option>19</option><option>20</option><option>21</option><option>22</option><option>23</option><option>24</option><option>25</option><option>26</option><option>27</option><option>28</option><option>29</option><option>30</option><option>31</option>
</select>
Month:
<select id="month">
<option selected="selected">month</option><option value="1">01</option><option value="2">02</option><option value="3">03</option><option value="4">04</option><option value="5">05</option><option value="6">06</option><option value="7">07</option><option value="8">08</option><option value="9">09</option><option>10</option><option>11</option><option>12</option>
</select>
Year: 
<select id="year">
<option selected="selected">year</option><option>2015</option><option>2014</option><option>2013</option><option>2012</option><option>2011</option><option>2010</option><option>2009</option><option>2008</option><option>2007</option><option>2006</option><option>2005</option><option>2004</option><option>2003</option><option>2002</option><option>2001</option><option>2000</option><option>1999</option><option>1998</option><option>1997</option><option>1996</option><option>1995</option><option>1994</option><option>1993</option><option>1992</option><option>1991</option><option>1990</option><option>1989</option><option>1988</option><option>1987</option><option>1986</option><option>1985</option><option>1984</option><option>1983</option><option>1982</option><option>1981</option><option>1980</option><option>1979</option><option>1978</option><option>1977</option><option>1976</option><option>1975</option><option>1976</option><option>1975</option><option>1976</option><option>1975</option><option>1974</option><option>1973</option><option>1972</option><option>1971</option><option>1970</option><option>1969</option><option>1968</option><option>1967</option><option>1966</option><option>1965</option><option>1964</option><option>1963</option><option>1962</option><option>1961</option><option>1960</option>
</select>
<br><br>
<span id="nr1"></span> / 
<span id="nr2"></span> / 
<span id="nr3"></span> / 
<span id="nr4"></span> / 
<span id="nr5"></span> / 
<span id="nr6"></span> / 
<span id="nr7"></span> / 
<span id="nr8"></span> / 
<span id="nr9"></span> / 
<span id="nr10"></span>
<br><br>
<button onclick="generate()">Generate</button>
<script type="text/javascript">
function generate() {
    // get user's birth date as a number
    var day = (document.getElementById("day").value);
    var month = (document.getElementById("month").value);
    var year = (document.getElementById("year").value);
    var dob = (day+month+year);
    //check if user specified dob
    if(isNaN(day) || isNaN(month) || isNaN(year))
   {
    alert("Check your birth date!");
   }
    else {
    // Sets Math.random to an ARC4-based PRNG that is autoseeded using the
    // current time, dom state, and other accumulated local entropy.
    // The generated seed string is returned.
    Math.seedrandom();
    //the custom seed is the user's dob
    nr1.textContent = (Math.random(dob));
    nr2.textContent = (Math.random(dob));
    nr3.textContent = (Math.random(dob));
    nr4.textContent = (Math.random(dob));
    nr5.textContent = (Math.random(dob));
    nr6.textContent = (Math.random(dob));
    nr7.textContent = (Math.random(dob));
    nr8.textContent = (Math.random(dob));
    nr9.textContent = (Math.random(dob));
    nr10.textContent = (Math.random(dob));
   }
}
</script>

我现在需要的是将所有的数字转换成整数,范围从1到50(类似于min max)。有人知道怎么做吗?>请随意使用:https://jsfiddle.net/cunuqq7h/9/