的Math.random() [英] Math.random()

查看:85
本文介绍了的Math.random()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我记得有一种编程语言可以初始化

随机数生成器,这样它就可以 - 如果你想 - 给你准确的

相同的序列每次使用相同的

参数初始化它时的随机数。可以用JavaScript完成吗?我在

文档中找不到任何内容。基本上,我想要实现的是为相同的给定初始化值始终获得相同的随机数序列(但是对于不同的初始值,当然不同的序列为
) 。


这可以用JavaScript完成吗?


问候,

Thomas

解决方案

你好


AFAIK,这不能用JavaScript完成,但允许使用

C,Pascal,...

要解决您的问题,请编写您自己的随机和随机函数:


var randSeed = 1234; //初始种子

函数随机化()

{

//使用math.random()生成随机数并分配给初始化

种子

}


函数随机()

{

randSeed =(randSeed * 1232)+(randSeed%212) - randSeed& 0xFA11;

//把你想要的任何论坛....我不是写伪编号的出口

发电机功能

}


生成相同的序列:

randSeed = 232; //你的初始种子

for(i = 0; i< 10; i ++)

document.write(random()+"< br>") ; //这将生成相同的序列!


每次生成一个新序列,调用randomize()或者只需将
randSeed变量初始化为您选择的值。


HTH,

Elias

" Thomas Mlynarczyk" < BL ************* @ hotmail.com>在消息中写道

新闻:c0 ************* @ news.t-online.com ...

我记得有一种编程语言,您可以在其中初始化
随机数生成器,以便它可以 - 如果您需要 - 每次使用相同的初始化时为您提供
完全相同的随机数序列
参数。可以用JavaScript完成吗?我在文件中找不到
中的任何内容。基本上,我想要实现的是为相同的给定初始化值
始终获得相同的随机数序列(当然,不同初始值的不同序列)。
这可以用JavaScript完成吗?

问候,
托马斯



也是如火如荼:

AFAIK,这不能用JavaScript完成


: - ((

randSeed =(randSeed * 1232)+(randSeed%212) - randSeed& 0xFA11;
//放任何你想要的论坛....我不是写出伪数据生成器函数的导出




好​​吧,我不需要任何统计上复杂的东西。背景是,

我正在编写一个游戏并且需要布置一些卡片一个随机的订单来开始玩。现在我希望能够稍后回到相同的布局

只需选择游戏编号12345&q uot;,其中12345将是种子,

应始终生成相同的牌组。目前我通过选择两个随机并交换他们的位置来随机洗牌

。这个我做了多次

次,所以所有卡都应该随机取代。


JRS:文章< c0 *************@ID-161723.news.uni-berlin.de> ;,见于

news:comp.lang.javascript,lallous< la * ****@lgwm.org>发布于星期一,16

2004年2月14:26:56: -


根据常见问题解答,回复应按照修剪后的报价进行;纠正。

" Thomas Mlynarczyk" < BL ************* @ hotmail.com>在消息中写道
新闻:c0 ************* @ news.t-online.com ...

我记得有一种编程语言在哪里你可以初始化
随机数生成器,这样它就可以 - 如果你愿意 - 每次用相同的
参数初始化它时,给你一个

相同的随机数序列。可以用JavaScript完成吗?我在


文档中找不到任何内容。基本上,我想要实现的是始终获得相同给定初始化值的相同序列的随机数


(但

当然不同的序列不同的init值)。

这可以用JavaScript完成吗?



AFAIK,这不能用JavaScript完成,但允许使用语言例如
C,Pascal,...


这不一定是语言的问题,但可能仅仅是

的实现的语言库。如果这里的任何人正在写一个新的

ECMA-262,那么随机种子需要成为一个读/写变量。

在测试中,它对于randoms是有用的可重复。

要解决您的问题,请编写您自己的随机和随机函数:

var randSeed = 1234; //初始种子
函数随机化()
//使用math.random()生成随机数并分配给初始种子
}

函数随机()
{
randSeed =(randSeed * 1232)+(randSeed%212) - randSeed& 0xFA11;
//把你想要的任何论坛....我不是写伪编号的出口
生成器函数
}



Nor拼写专家;但是,从TZ +0200,这并不重要。


Donald E Knuth写道: - "选择

方法时不应生成随机数随意地。选择一个好的算法是非常重要的,除了那些愿意查找可用资源而不是
重新发明轮子的人之外,它们是b $ b。


参见

< URL:http://www.merlyn.demon.co.uk/pas-rand.htm>

< URL:http ://www.merlyn.demon.co.uk/js-randm.htm>

和Knuth的卷。


报告以下内容好(X [n]是连续的RandSeed): -


X [n + 1] = 134775813 * X [n] + 1(mod 2 ^ 32)

X [n + 1] = 1664525 * X [n] + 1013904223(mod 2 ^ 32)


除以2 ^ 32,当然。


lallous的表达实际上非常糟糕;初始化为

1234,它立即进入一个长度为4的循环。使用1,很快

达到一个长度为2的循环。


0xFA11 = 1111 1010 0001 0001,设置为8位;实际上,我们有一个8位种子,最大周期为256.


-

?约翰斯托克顿,英国萨里。 ?@merlyn.demon.co.uk Turnpike v4.00 IE 4?

< URL:http://jibbering.com/faq/> Jim Ley的新闻常见问题解答:comp.lang.javascript

< URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr数学,日期,来源。

< URL:http://www.merlyn.demon.co.uk/> TP / BP / Delphi / jscr /& c,常见问题项目,链接。


I remember there is a programming language where you can initialize the
random number generator, so that it can - if you want - give you the exactly
same sequence of random numbers every time you initialize it with the same
parameter. Can this be done with JavaScript? I couldn''t find anything in the
documentation. Basically, what I want to achieve is to obtain always the
same sequence of random numbers for the same given initialization value (but
of course different sequences for different init values).

Can this be done in JavaScript?

Greetings,
Thomas

解决方案

Hello

AFAIK, This cannot be done w/ JavaScript, but allowed with languages such as
C, Pascal, ...

To solve your problem, write your own random and randomize functions as:

var randSeed = 1234; // initial seed
function randomize()
{
// use math.random() to generate a random number and assign to initial
seed
}

function random()
{
randSeed = (randSeed * 1232) + (randSeed % 212) - randSeed & 0xFA11;
// put any forumla you want....i am not an export at writing pseudo number
generators functions
}

To generate same sequence:
randSeed = 232; // your initial seed
for (i=0;i<10;i++)
document.write(random() + "<br>"); // this will generate same sequence!

To generate a new sequence everytime, call randomize() or simply init
randSeed variable to a value of your choice.

HTH,
Elias
"Thomas Mlynarczyk" <bl*************@hotmail.com> wrote in message
news:c0*************@news.t-online.com...

I remember there is a programming language where you can initialize the
random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you initialize it with the same
parameter. Can this be done with JavaScript? I couldn''t find anything in the documentation. Basically, what I want to achieve is to obtain always the
same sequence of random numbers for the same given initialization value (but of course different sequences for different init values).

Can this be done in JavaScript?

Greetings,
Thomas



Also sprach lallous:

AFAIK, This cannot be done w/ JavaScript
:-(((
randSeed = (randSeed * 1232) + (randSeed % 212) - randSeed & 0xFA11;
// put any forumla you want....i am not an export at writing pseudo
number generators functions



Well, I don''t need anything statistically sophisticated. The background is,
I''m programming a game and need to lay out some cards in a random order to
start playing. Now I want to be able to get back to the same layout later by
simply selecting "game number 12345", where 12345 will be the seed that
should always generate the same deck of cards. Currently I shuffle the cards
by selecting two at random and swapping their places. This I do a number of
times, so more or less all the cards should be randomly displaced.


JRS: In article <c0*************@ID-161723.news.uni-berlin.de>, seen in
news:comp.lang.javascript, lallous <la*****@lgwm.org> posted at Mon, 16
Feb 2004 14:26:56 :-

Responses should go after trimmed quotes, as per FAQ; corrected.

"Thomas Mlynarczyk" <bl*************@hotmail.com> wrote in message
news:c0*************@news.t-online.com...

I remember there is a programming language where you can initialize the
random number generator, so that it can - if you want - give you theexactly

same sequence of random numbers every time you initialize it with the same
parameter. Can this be done with JavaScript? I couldn''t find anything in


the

documentation. Basically, what I want to achieve is to obtain always the
same sequence of random numbers for the same given initialization value


(but

of course different sequences for different init values).

Can this be done in JavaScript?


AFAIK, This cannot be done w/ JavaScript, but allowed with languages such as
C, Pascal, ...
It is not necessarily a matter of the language, but may be just of the
implementation of the language library. If anyone here is writing a new
ECMA-262, then the random seed needs to be made a read/write variable.
In test, it is useful for randoms to be reproducible.
To solve your problem, write your own random and randomize functions as:

var randSeed = 1234; // initial seed
function randomize()
{
// use math.random() to generate a random number and assign to initial
seed
}

function random()
{
randSeed = (randSeed * 1232) + (randSeed % 212) - randSeed & 0xFA11;
// put any forumla you want....i am not an export at writing pseudo number
generators functions
}


Nor an expert at spelling; but, from TZ +0200, that is not important.

Donald E Knuth wrote :- "Random numbers should not be generated with a
method chosen at random". Choice of a good algorithm is non-trivial,
except for those willing to look up available resources rather than
reinvent the wheel themselves.

See
<URL:http://www.merlyn.demon.co.uk/pas-rand.htm>
<URL:http://www.merlyn.demon.co.uk/js-randm.htm>
and Knuth''s volumes.

The following are reported good (X[n] are successive RandSeed) :-

X[n+1] = 134775813*X[n] + 1 (mod 2^32)
X[n+1] = 1664525*X[n] + 1013904223 (mod 2^32)

Divide by 2^32, of course.

The expression of lallous is, in fact, remarkably bad; initialised with
1234, it immediately enters a cycle of length 4. With 1, it soon
reaches a cycle of length 2.

0xFA11 = 1111 1010 0001 0001, with 8 bits set; we have, in effect, an
8-bit seed and a maximum cycle length of 256.

--
? John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ?
<URL:http://jibbering.com/faq/> Jim Ley''s FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


这篇关于的Math.random()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆