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

时间:2015-06-10 08:19:07

标签: javascript random numbers generator seed

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

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

我们走了:

所以我想要的是生成10个不同的数字,用“ - ”(减号)分隔,每个数字的范围是1-50,我希望每个数字的种子通过乘以2个来组成: / p>

  1. 当前时间与更大的数字序列一样多(每个数字延迟1秒,所以时间会有所不同)
  2. 您的出生日期(来自3个选择输入)
  3. [!] 此外,我想知道如何以不同的方式为这10个数字的生成制作动画,例如...例如老火车站Arival / Departures机械显示的动画,或者喜欢不同的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>

2 个答案:

答案 0 :(得分:0)

如果您在提供的代码中修复了product_id = '2',那么Javascript代码实际上可以正常工作。您没有任何事件处理程序可以使用您提供的HTML来控制它。

mysql> select * from `products`;
+----+-------+
| id | name  |
+----+-------+
|  1 | prod1 |
|  2 | prod2 |
+----+-------+

mysql> select * from `images`;
+----+------------+----------+---------+
| id | product_id | image    | default |
+----+------------+----------+---------+
|  1 |          1 | img1.png |       0 |
|  2 |          1 | img2.png |       0 |
|  3 |          2 | img3.png |       0 |
|  4 |          1 | img4.png |       1 |
|  5 |          2 | img5.png |       0 |
|  6 |          1 | img6.png |       0 |
|  7 |          1 | img7.png |       0 |
|  8 |          2 | img8.png |       0 |
|  9 |          1 | img9.png |       0 |
+----+------------+----------+---------+
SyntaxError

A further example of using the library that you linked to.

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>

答案 1 :(得分:0)

这与我使用David Bau的脚本(seedrandom.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)。谁知道怎么样? &GT;随意使用:https://jsfiddle.net/cunuqq7h/9/