Original link: https://geniuspeng.github.io/2016/09/12/js-random/
Recently, there is a need to generate a random number, but it cannot be completely random. That is to say, a seed seed is needed. When the seed is unchanged, the random number will remain unchanged. Different random numbers will be generated according to different seeds = = Anyway It's a pseudo-random number. I thought about it for a long time and couldn't find a good solution, so I checked the Internet, and there really is such a thing~~
Well, it looks like this, the link to know is here: https://www.zhihu.com/question/22818104
1 2 3 4 5 6 7 8 9 10 11 12 |
function rnd ( seed ) { seed = ( seed * 9301 + 49297 ) % 233280 ; //Why use these three numbers? return seed / ( 233280.0 );}; function rand(number){ today = new Date(); seed = today.getTime(); returnMath.ceil( rnd( seed ) * number );}; myNum=(rand(5)); |
He also wrote this note, why are these three numbers