I am starting to learn JavaScript and I found this article: http://michalbe.blogspot.ro/2011/02/javascript-random-numbers-with-custom.html
I liked the idea of a custom seed number generator but I for the love of Thor cannot figure it out, I really need a practical example done with pure javascript or a library like jQuery if it's easier.
Here we go:
So what I want is to generate 10 different numbers sepparated by "-" (minus), each number with the range of 1-50 and I would like the seed for each number to be made up by multiplying 2 things:
- the current time in as much as a bigger number sequence as you can get it (with a delay of 1 second for each number so the time will be different)
- your birth date (from 3 select inputs)
[!] Also I want to know how we can animate the generation of these 10 numbers in different ways, like... for example the animation of old train station Arival/Departures mechanical displays, or like different HTML5 canvas particle techniques or CSS3 or anything really. - This can be done in another question if you think it's too much.
If you are able to help me out with this I will be so very forever grateful!
Thank you!
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>
If you fix the
SyntaxErrorin your supplied code, then the Javascript code actually works. You don't have any event handlers to control it with your supplied HTML though.A further example of using the library that you linked to.