JavaScript pseudo-random sequence generator

2024/2/27 8:55:37

I need to generate a deterministic (i.e. repeatable) sequence of pseudo-random numbers given an initial seed and select the nth item from that sequence.

If JavaScript's random function was seedable, I could just do:

function randomNth(seed, seq)
{var r;Math.randomSeed(seed);for (var i = 0; i++ < seq; i++){r = Math.random();}return r;
}

However, it's not, and alternative, seedable PRNGs look to be a little slow; asking for the 250th number would be expensive.

I think a hash is what I want here, perhaps something like md5(seed + seq) % max but JavaScript doesn't have md5() and if I'm doing it in code there's probably a better choice of hash.

I'd like a function where

x = randomNth(seed, seq, maxVal) // x is int && x >= 0 && x < maxVal

or, ideally

x = randomNth(seed, seq) // x >= 0 && x < 1, same as Math.random()

Other requirements:

  • must run in node.js and in a browser
  • numbers should be statistically random (or close enough as the period will be small)
  • should be O(1) and reasonably performant
Answer

There are some good int -> int hashing functions on this page you can use one of them.

function hash(a)
{a = (a+0x7ed55d16) + (a<<12);a = (a^0xc761c23c) ^ (a>>19);a = (a+0x165667b1) + (a<<5);a = (a+0xd3a2646c) ^ (a<<9);a = (a+0xfd7046c5) + (a<<3);a = (a^0xb55a4f09) ^ (a>>16);if( a < 0 ) a = 0xffffffff + a;return a;
}
var seed = 26254;
var index = 250;
alert( hash( seed + index ) );
http://en.ppmy.cn/q/41233.html

Related Q&A

Google Maps Marker title no longer appears as tooltip on hover

The Google Maps documentation indicates that adding a title property to a Marker object will result in its value appearing as a tooltip when the user hovers over that Marker. This used to work perfect…

How to find where browser breaks a paragraph of text

I need to add line breaks in the positions that the browser naturally adds a newline in a paragraph of text.For example:<p>This is some very long text \n that spans a number of lines in the parag…

Extract frame of gif with graphicsmagick in node.js

I am trying to convert a gif into a png using graphicsmagick on node.js. In their documentation they have the following code:// pull out the first frame of an animated gif and save as png gm(/path/to/a…

Regex for number between 0 to 100?

It should not allow leading zeroes, like 099. Allowed values should be like these: 35, 99, 1, 2, 3, 100.This is What I tried: $(#createCoupon_discountAmount).bind(input propertychange, function () {$(t…

Set text in textarea, including newlines

I want to set the text in a <textarea> from a js-function; Im simply setting the innerText-attribute to a new value. The text is multiline and should be displayed as such. However, newlines are d…

CSS column-count elements jumping across columns

Im trying to get a dynamic amount of elements to show across 5 elements using CSS3 column-count, but when I expand the list item heights on hover, it occasionally causes jumping (an element going to th…

I have no idea Object(this) means

In https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/fillthere is a line like// Steps 1-2. if (this == null) {throw new TypeError(this is null or not defined); }var O …

How to combine two parts of single form in angular?

I am making angular application with angular dynamic form where i am loading data for dynamic form through JSON..JSON has two parts such as part 1 and part 2,jsonDataPart1: any = [{"elementType&qu…

jquery hide a div that contains flash without resetting it

Greetings, I have written a modal using jquery UI and it appears at the front of a flash movie thus the html inside the modal becomes corrupt, I tried to hide the movie right before modal gets triggere…

jstree jquery plugin - Get all child and sub child nodes of parent

I am using the jsTree jquery plugin I am trying to fetch all the nodes and sub nodes of a selected parent. But somehow its not working unless I use recursion. Is there a better way inherent to jsTre…