js中math.random 种子()方法返回的是什么范围的值?

Math.random() -- 返回0和1之间的伪随机数
random,中文"随机的"的意思
random函数语法
Math.random();
random函数参数
random函数返回值
返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1)
random函数示例
document.write(Math.random());
返回随机数
document.write(Math.random()*(20-10)+10);
返回10-20的随机数
document.write(Math.random()*(n-m)+m);
返回指定范围的随机数(m-n之间)的公式
js生成随机数
&script language="JavaScript"&
function rndNum(n)
var rnd="";
for(var i=0;i&n;i++)
rnd+=Math.floor(Math.random()*10);
alert(rnd);
//--&&/script&
也许你会喜欢和关注与本文内容高度相关的文章:
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)数学对象Math是js的内置对象,无需被创建,因此不必被实例化,他只有静态属性和静态方法Math对象属性:1.E 属性 E 属性约等于 2.718。2.LN2 属性返回 2 的自然对数。3.PI 属性 返回圆的周长与其直径的比值,约等于 3.793。4.LN10 属性返回 10 的自然对数。5.LOG2E 属性返回 以 2 为底 e(自然对数的底)的对数。6.LOG10E 属性返回以 10 为底 e(自然对数的底)的对数7.SQRT1_2 属性返回 0.5 的平方根,或说 2 的平方根分之一。8.SQRT2 属性返回 2 的平方根。&Math对象的方法:1.abs() &返回数值的绝对值语法示例: Math.abs(num)2.提供了一组用于计算数值的三角函数语法示例: Math.cos(num)3.Math提供了三种方法用于数值的舍入运算(1).Math.ceil(num) & 返回大于或等于num的最小整数(2).Math.floor(num) & 返回小于或等于num的最大整数(3).Math.round() & &对num的小数位进行四舍五入的整数4.random()产生0和1之间的随机小数语法示例:Math.random(num) 可以限制范围 &Math.round(Math.random()*(n-m)+n)返回m到n的随机整数5.max()返回给出多个值中的最大数语法示例:Math.max(num1,num2,...)6.min()返回给出值中最小数语法示例:Math.min(num1,num2,...)7.pow(m,n)幂运算 m是幂运算的基数 &n是幂运算的指数语法示例:Math.pow(2,3)8.对数运算与开平方运算log(n) 计算以e为底n的自然对数&sqrt(n) &计算n的平方根演示代码: &script&
//math属性
with(document){
write("Math.E="+Math.E+"&br&");
write("Math.LN2 ="+Math.LN2 +"&br&");
write("Math.PI ="+Math.PI +"&br&");
//math方法
with(document){
write("Math.abs(-100)="+Math.abs(-100)+"&br&");
write("Math.cos(0)="+Math.cos(0)+"&br&");
write("Math.ceil(10.40)="+Math.ceil(10.40)+"&br&");
write("Math.floor(10.40)="+Math.floor(10.40)+"&br&");
write("Math.round(10.40)="+Math.round(10.50)+"&br&");
write("Math.random()="+Math.random()+"&br&");
write("Math.max(1,5,0)="+Math.max(1,5,0)+"&br&");
write("Math.min(1,5,0)="+Math.min(1,5,0)+"&br&");
write("Math.pow(2,3)="+Math.pow(2,3)+"&br&");
write("Math.log(2)="+Math.log(2)+"&br&");
write("Math.sqrt(2)="+Math.sqrt(2)+"&br&");
&/script&运行结果如下:Math.E=2.045Math.LN2 =0.9453Math.PI =3.793Math.abs(-100)=100Math.cos(0)=1Math.ceil(10.40)=11Math.floor(10.40)=10Math.round(10.40)=11Math.random()=0.8909Math.max(1,5,0)=5Math.min(1,5,0)=0Math.pow(2,3)=8Math.log(2)=0.9453Math.sqrt(2)=1.09511231
关注微信公众平台js中Math.random()生成指定范围数值的随机数-js教程-网页制作-壹聚教程网js中Math.random()生成指定范围数值的随机数
Math.random函数就不像php的rand函数一样可以生成指数范围的数据了,math.random只是生成了一个伪随机数,之后还要经过我们处理才行哦。
今天有又网友问到我 JavaScript 怎么生成指定范围数值随机数。Math.random() 这个方法相信大家都知道,是用来生成随机数的。不过一般的参考手册时却没有说明如何用这个方法来生成指定范围内的随机数。这次我就来详细的介绍一下Math.random(),以及如何用它来生成制定范围内的随机数。
w3school的random()教程
定义和用法
random() 方法可返回介于 0 ~ 1 之间的一个随机数。
Math.random()
0.0 ~ 1.0 之间的一个伪随机数。
在本例中,我们将取得介于 0 到 1 之间的一个随机数:
&script type=&text/&&
document.write(Math.random());
如何生成指定范围值的随机数
看完w3school的教程,应该知道Math.random()方法的基本用法了。
利用 parseInt()、Math.floor() 或者 Math.ceil()进行四舍五入处理
我们看到,直接使用Math.random()方法,生成的是一个小于1的数,所以:
Math.random()*5
得到的结果是一个小于5的随机数。而我们通常希望得到的是0-5之间的整数,所以我们需要对得到的结果四舍五入处理一下,从而得到我们期望的整数。parseInt()、Math.floor()和Math.ceil()都可以起到四舍五入的作用。
var randomNum = Math.random()*5;
alert(randomNum); // 2.1183&
alert(parseInt(randomNum,10)); // 2
alert(Math.floor(randomNum)); // 2
alert(Math.ceil(randomNum)); // 3
由测试的代码我们可以看到,parseInt()和Math.floor()的效果是一样的,都是向下取整数部分。所以parseInt(Math.random()*5,10)和Math.floor(Math.random()*5)都是生成的0-4之间的随机数,Math.ceil(Math.random()*5)则是生成的1-5之间的随机数。
生成指定范围数值随机数
所以,如果你希望生成1到任意值的随机数,公式就是这样的:
// max - 期望的最大值
parseInt(Math.random()*max,10)+1;
Math.floor(Math.random()*max)+1;
Math.ceil(Math.random()*max);
如果你希望生成0到任意值的随机数,公式就是这样的:
// max - 期望的最大值
parseInt(Math.random()*(max+1),10);
Math.floor(Math.random()*(max+1));
如果你希望生成任意值到任意值的随机数,公式就是这样的:
// max - 期望的最大值
// min - 期望的最小值
parseInt(Math.random()*(max-min+1)+min,10);
Math.floor(Math.random()*(max-min+1)+min);
怎么样?现在应该很清楚如何去生成你需要随机数了吧?!希望看完这篇文章对你的开发有帮助!这次就到这里了!
上一页: &&&&&下一页:相关内容Javascript里Math.random()产生的随机数的规律是什么?
官方文档是这么写的The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range.如果说我想要在测试的时候预测要产生的随机数是什么要怎么做呢?另外,使用这样的伪随机数来生成密码串是否有安全性的问题?
按投票排序
想要固定序列可以用 MT19937:/*
I've wrapped Makoto Matsumoto and Takuji Nishimura's code in a namespace
so it's better encapsulated. Now you can have multiple random number generators
and they won't stomp all over eachother's state.
If you want to use this as a substitute for Math.random(), use the random()
method like so:
var m = new MersenneTwister();
var randomNumber = m.random();
You can also call the other genrand_{foo}() methods on the instance.
If you want to use a specific seed in order to get a repeatable random
sequence, pass an integer into the constructor:
var m = new MersenneTwister(123);
and that will always produce the same random sequence.
Sean McCullough ()
A C-program for MT19937, with initialization improved .
Coded by Takuji Nishimura and Makoto Matsumoto.
Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Any feedback is very welcome.
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
var MersenneTwister = function(seed) {
if (seed == undefined) {
seed = new Date().getTime();
/* Period parameters */
this.N = 624;
this.M = 397;
this.MATRIX_A = 0x9908b0df; /* constant vector a */
this.UPPER_MASK = 0x; /* most significant w-r bits */
this.LOWER_MASK = 0x7fffffff; /* least significant r bits */
this.mt = new Array(this.N); /* the array for the state vector */
this.mti = this.N + 1; /* mti==N+1 means mt[N] is not initialized */
this.init_genrand(seed);
/* initializes mt[N] with a seed */
MersenneTwister.prototype.init_genrand = function(s) {
this.mt[0] = s &&& 0;
for (this.mti = 1; this.mti & this.N; this.mti++) {
var s = this.mt[this.mti - 1] ^ (this.mt[this.mti - 1] &&& 30);
this.mt[this.mti] = (((((s & 0xffff0000) &&& 16) * ) && 16) + (s & 0x0000ffff) * ) + this.mti;
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
/* In the previous versions, MSBs of the seed affect
/* only MSBs of the array mt[].
modified by Makoto Matsumoto
this.mt[this.mti] &&&= 0;
/* for &32 bit machines */
/* initialize by an array with array-length */
/* init_key is the array for initializing keys */
/* key_length is its length */
/* slight change for C++,
MersenneTwister.prototype.init_by_array = function(init_key, key_length) {
var i, j, k;
this.init_genrand();
k = (this.N & key_length ? this.N : key_length);
for (; k; k--) {
var s = this.mt[i - 1] ^ (this.mt[i - 1] &&& 30)
this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) &&& 16) * 1664525) && 16) + ((s & 0x0000ffff) * 1664525))) + init_key[j] + j; /* non linear */
this.mt[i] &&&= 0; /* for WORDSIZE & 32 machines */
if (i &= this.N) {
this.mt[0] = this.mt[this.N - 1];
if (j &= key_length) j = 0;
for (k = this.N - 1; k; k--) {
var s = this.mt[i - 1] ^ (this.mt[i - 1] &&& 30);
this.mt[i] = (this.mt[i] ^ (((((s & 0xffff0000) &&& 16) * ) && 16) + (s & 0x0000ffff) * )) - i; /* non linear */
this.mt[i] &&&= 0; /* for WORDSIZE & 32 machines */
if (i &= this.N) {
this.mt[0] = this.mt[this.N - 1];
this.mt[0] = 0x; /* MSB is 1; assuring non-zero initial array */
/* generates a random number on [0,0xffffffff]-interval */
MersenneTwister.prototype.genrand_int32 = function() {
var mag01 = new Array(0x0, this.MATRIX_A);
/* mag01[x] = x * MATRIX_A
for x=0,1 */
if (this.mti &= this.N) { /* generate N words at one time */
if (this.mti == this.N + 1) /* if init_genrand() has not been called, */
this.init_genrand(5489); /* a default initial seed is used */
for (kk = 0; kk & this.N - this.M; kk++) {
y = (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK);
this.mt[kk] = this.mt[kk + this.M] ^ (y &&& 1) ^ mag01[y & 0x1];
for (; kk & this.N - 1; kk++) {
y = (this.mt[kk] & this.UPPER_MASK) | (this.mt[kk + 1] & this.LOWER_MASK);
this.mt[kk] = this.mt[kk + (this.M - this.N)] ^ (y &&& 1) ^ mag01[y & 0x1];
y = (this.mt[this.N - 1] & this.UPPER_MASK) | (this.mt[0] & this.LOWER_MASK);
this.mt[this.N - 1] = this.mt[this.M - 1] ^ (y &&& 1) ^ mag01[y & 0x1];
this.mti = 0;
y = this.mt[this.mti++];
/* Tempering */
y ^= (y &&& 11);
y ^= (y && 7) & 0x9d2c5680;
y ^= (y && 15) & 0xefc60000;
y ^= (y &&& 18);
return y &&& 0;
/* generates a random number on [0,0x7fffffff]-interval */
MersenneTwister.prototype.genrand_int31 = function() {
return (this.genrand_int32() &&& 1);
/* generates a random number on [0,1]-real-interval */
MersenneTwister.prototype.genrand_real1 = function() {
return this.genrand_int32() * (1.0 / .0);
/* divided by 2^32-1 */
/* generates a random number on [0,1)-real-interval */
MersenneTwister.prototype.random = function() {
return this.genrand_int32() * (1.0 / .0);
/* divided by 2^32 */
/* generates a random number on (0,1)-real-interval */
MersenneTwister.prototype.genrand_real3 = function() {
return (this.genrand_int32() + 0.5) * (1.0 / .0);
/* divided by 2^32 */
/* generates a random number on [0,1) with 53-bit resolution*/
MersenneTwister.prototype.genrand_res53 = function() {
var a = this.genrand_int32() &&& 5,
b = this.genrand_int32() &&& 6;
return (a *
+ b) * (1.0 / 0992.0);
/* These real versions are due to Isaku Wada,
ECMAScript没有规定死要用什么算法来实现Math.random(),只规定了返回值的范围。相关规范:random ( )Returns a Number value with positive sign, greater than or equal to 0 but less than 1, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-dependent algorithm or strategy. This function takes no arguments.于是各家实现都不一样。无论是初始化伪随机数数列的种子的方式,还是生成伪随机数的算法,都不一样。FireFox / SpiderMonkey:Chrome / V8:Safari / JavaScriptCore:Rhino:JDK8u / Nashorn:&- 这俩用Java实现的JavaScript引擎都直接调用Java的java.lang.Math.random()。但不同JDK里这个方法的实现方式也未必一样。所以题主想要“预测”随机数不是件易事嗯…看到后面两个回答发现我的回答似乎偏离了题主的意图——题主需要的是像他们说的把Math.random()覆盖掉使它变得可预测的办法。不过我不打算在我的回答里加上这方面的实现了,让它自然沉吧…
测试时可用自行定义的函数,得到想要的固定序列。function makeRandom(seed) {
return function() {
seed = (seed + 0.125) % 1.0;
return seed;
Math.random = makeRandom(0.0);
「使用这样的伪随机数来生成密码串是否有安全性的问题?」有。可以用这个 ,不过未完成标准化。
建议不要直接调用随机函数。我一般是直接包一层,然后这里面放入各种测试开关。同理为了测试方便,尽量不要直接用结果不可控的 API 比如 date
线性同余法吧.....只要第一个初始值(种子)确定,公式确定,随机数列就是固定的吧....
我认为自然界宏观上是没有真随机的(微观上好像说原子的什么放射性衰变是真随机,不大清楚了),比如抛一个硬币,计算好抛出的角度,力,硬币重量,硬币重心,空气阻力,当地重力加速度等等等等,那么一定能得到你想要的答案,计算机上的随机数都是一整套伪随机数算法,首先会存储一些熵,用来生成随机数,熵的来源可以是你某次敲击键盘的时间,或者是当时的电压电流什么的,总之就是和环境有关,现在都是依靠处理器芯片提供的熵,不过都是黑盒,外人无从得知,前段时间貌似英特尔被指控说提供熵的芯片存在后门什么的
已有帐号?
无法登录?
社交帐号登录

我要回帖

更多关于 math.random 种子 的文章

 

随机推荐