Computer Science Canada

Javascript pseudo-random with a given seed

Author:  DtY [ Fri Jun 18, 2010 7:45 pm ]
Post subject:  Javascript pseudo-random with a given seed

Is there a javascript function that I can give a specific seed and get a pseudorandom number that will always be the same for that seed?

What I'm wanting to do is pick a random colour for a certain string, and have it be the same each time, for that string.

Author:  copthesaint [ Sat Jun 19, 2010 1:10 am ]
Post subject:  RE:Javascript pseudo-random with a given seed

So what basically, when a string is given like "hello" it will sent a random number like 42, and it will always be 42 if "hello" if given. then you run again and maybe "hello" is 67 this time?

Author:  DtY [ Sat Jun 19, 2010 8:53 am ]
Post subject:  Re: RE:Javascript pseudo-random with a given seed

copthesaint @ Sat Jun 19, 2010 1:10 am wrote:
So what basically, when a string is given like "hello" it will sent a random number like 42, and it will always be 42 if "hello" if given. then you run again and maybe "hello" is 67 this time?
Yes, except that when you give "hello", it would always give 42, I assume you meant to put something else when you said "hello" to give 67

[edit] This isn't too important to me now, instead of randomly picking the colours, I decided to make it based on the order stuff was in, which would always stay the same, but I'm still curious.

Author:  DemonWasp [ Sat Jun 19, 2010 1:43 pm ]
Post subject:  RE:Javascript pseudo-random with a given seed

A few minutes of google gave me this, which seems to indicate that there's no built-in function, but also provides code to supply such a function yourself.

Author:  2goto1 [ Sat Jun 19, 2010 8:38 pm ]
Post subject:  Re: Javascript pseudo-random with a given seed

Hi DtY, Math.random() does accept a seed but even with the same seed (i.e calling Math.random(2) several times), the resultant value is never the same. Unlike Java, C++, C#, etc. JavaScript objects don't support any sort of underlying hash key representing their value (i.e. where "Hello" might have a hash code that always calculates to 42, etc). I don't think the Object type has any sort of getHashCode() function.

There are a lot of do-it-yourself ideas that you can find from Google. The simplest idea that I can think of is to maintain a global JavaScript Array variable. Depending on how many random values you need to maintain in your JavaScript application it might not be the right fit. Here's an example for thought:

code:
var randValues = new Array();

alert(getRandomValue("Hello"));
alert(getRandomValue("Hello"));

function getRandomValue(key)
{
  if (randValues[key] == undefined)
  {
    randValues[key] = Math.random(key);
  }
  return randValues[key];
}


As you can see, I'm using a standard JavaScript Array as an associative (key-value pair) lookup. This use of Arrays as associative arrays is itself a hotly debated topic - many people think it's poor programming practice - so I'd suggest if you're interested in using it further to read up on the debate and the alternative approaches such as a custom JavaScript map solution (of which there are a lot out there already but it never hurts to create a new one for learning purposes!).

Hope that helps a bit with some food for thought!
2goto1

Author:  DemonWasp [ Sun Jun 20, 2010 1:02 pm ]
Post subject:  RE:Javascript pseudo-random with a given seed

2goto1: In javascript, you can have excess parameters in a function call and they will simply be ignored. Math.random() does not accept any parameters, so it's entirely ignoring anything you pass to it. If DtY really wants this (somewhat useful) functionality, he'll need to either write it himself or find someone else's pseudorandom library.

Author:  DtY [ Sun Jun 20, 2010 1:24 pm ]
Post subject:  RE:Javascript pseudo-random with a given seed

So javascript does not have anyway to do this built in?

I wont bother writing my own then, it will take to long to write, and then will be much slower than I need.

Thanks everyone, though.

Author:  2goto1 [ Sun Jun 20, 2010 1:42 pm ]
Post subject:  RE:Javascript pseudo-random with a given seed

Good to know Demonwasp thanks! Yep looks like a roll your own or use someone else's situation then, if it is necessary to produce the same random value for a given string from session to session.


: