[sdiy] Random Number Generation by Lookup table : The Code
David Hughes
dhughes at globalnet.co.uk
Fri Nov 22 12:01:38 CET 2002
Folks,
For those who e-mailed asking for the code... here's the browser stuff...
It produces 2K's worth of byte-sized comma-seperated values... (I also loaded
the file into Excell and did a scatter plot to check for randomness... ?:) )
> <SCRIPT LANGUAGE="Javascript">
> <!-- Hide from non-Javascript browsers
>
> DisplayRandomNumber(2048, 0, 255);
>
>
> /**************************************************************************************/
>
> function DisplayRandomNumber(RandomNumberCount, MinimumNumber, MaximumNumber)
> {
> while (RandomNumberCount-- > 0)
> {
> ThisNumber = RandomNumber(MaximumNumber);
> document.writeln(ThisNumber, ", ");
> }
> }
>
> /**************************************************************************************/
>
> function RandomNumber(SeedValue)
> {
> var MyNumber = 0;
>
> MyNumber = Math.random();
> MyNumber = Math.round(MyNumber * SeedValue);
> return MyNumber;
> }
>
> /**************************************************************************************/
>
> //-->
> </SCRIPT>
Wrap it in HTML to make it work...
And here's the table look-up code...
Call UpdateRandomNumberIndex() every time an event occurs - especially
asynchronous events - and the behaviour should be pretty random even though it's table-based.
> /*************************************************************************************/
>
> /* UpdateRandomNumberIndex() is called to reset the index into the random number table.
> We do this one a regular basis but take the number at the current index position and
> add it to the index, wrapping back to the start if we hit the top of the table. This
> should give us completely random behavior.
> */
>
> void UpdateRandomNumberIndex()
> {
> unsigned char TableEntry;
>
> TableEntry = RandomNumberTable[RandomNumberIndex];
> RandomNumberIndex += TableEntry;
> if (RandomNumberIndex > _NUMBER_OF_RANDOM_TABLE_ENTRIES)
> RandomNumberIndex = RandomNumberIndex - _NUMBER_OF_RANDOM_TABLE_ENTRIES;
> }
>
> /*************************************************************************************/
>
> /* Random() is called to generate a random number in the range of 0..100 inclusive.
> */
>
> unsigned int Random()
> {
> UpdateRandomNumberIndex();
> return RandomNumberTable[RandomNumberIndex];
> }
>
> /*************************************************************************************/
>
> /* GetRandomNumberInRange() is called to return a random number in the range specified
> by the calling parameters.
> */
>
> unsigned int GetRandomNumberInRange(unsigned int MinimumValue, unsigned int MaximumValue)
> {
> unsigned int RandomNumber;
> unsigned int Range;
>
> Range = MaximumValue - MinimumValue + 1;
> RandomNumber = Random();
> RandomNumber = ((RandomNumber * Range) / _RANDOM_NUMBER_RANGE) + MinimumValue;
> return RandomNumber;
> }
>
> /*************************************************************************************/
As you can see, it ain't rocket-science and it's fast...
Regards
David
-----------------------------------------------------------------------------------
Infection Music
http://www.infectionmusic.co.uk
-----------------------------------------------------------------------------------
More information about the Synth-diy
mailing list