Results 1 to 4 of 4

Thread: Seeded Random

  1. #1
    Animator / Artist Sculley's Avatar
    Join Date
    Feb 2003
    Location
    England
    Posts
    159

    Seeded Random

    Anyone know any good implementations for LScript returning -1 to 1 or 0 to 1 random values based on a seed?

    Help is appreciated.

    Stephen

  2. #2
    Super Member Kryslin's Avatar
    Join Date
    Feb 2009
    Location
    Prescott, IA
    Posts
    1,461
    You could set up a simple linear-congruential generator in lscript...

    Code:
    __x0 = 0;  //This is where your seed goes, and it gets cycled by the random function...
    
    randomize: q
    {
       //Set your seed number
    __x0 = q;
    }
    
    random:q
    {
       //q does nothing.
       a = 214013;  //Big Prime Number
       c = 2531011;  //Bigger Prime Number
       x1 = ( x0 * a + c ) % 2^^24;
       __x0 = x1;
       //Return a floating point value between 0...1
       //Just like ol' BASIC's RND function...
       x1 /= 2^^24;
       return(x1);
    }
    Call randomize to seed it, then use random to get your numbers. Cycle it a couple hundred - couple thousand times to get a really random number.


    For a range of -1 to 1, multiply by 2, subtract 1.
    --------
    My Scripts for Lightwave
    Intel Core i7 960 @3.20 Ghz, 24 GB ram, EVGA 6GB GTX980Ti "Classified" driving 2 x HP LA2405.

  3. #3
    Animator / Artist Sculley's Avatar
    Join Date
    Feb 2003
    Location
    England
    Posts
    159
    Many thanx, its the same code I ended up working with as well.

    Code:
    // RANDOM
    
    _randomseed = 0; // n Seed
    
    randomseed: seed
    {
        _randomseed = seed;
    }
    
    random
    {
        n = (_randomseed * 214013 + 2531011) % 2^^24;
        _randomseed = n;
        n /= 2^^24; // 0..1
        return(n);
    }

  4. #4
    Hi,

    Simpler solution :

    initialize your seed with :
    Code:
    randu(seed);
    then always use randu() to generate your random numbers:

    Code:
    custom_random: mi, ma
    {
    	var value = mi + randu() * (ma - mi);
    	if (mi.isInt())
    	{
    		return round( value, 0 );
    	}
    	else
    	{
    		return value;
    	}
    }
    
    // bonus function : this one will return a random number which will tend towards "mi" or "ma" according to "bia",
    // with bia=-1 you'll get more random values near "mi" and with bia = 1 you'll get more random values near "ma",
    // with bia = 0, you'll get the default balanced andom results.
    // of course you can use any float value between -1 and 1 for the bias.
    random_bias: mi, ma, bia
    {
    	var maxval = 1000.0;
    	var maxilog = log( maxval );
    	if (bia < -1)
    	{
    		bia = -1;
    	}
    	if (bia > 1)
    	{
    		bia = 1;
    	}
    	var r = custom_random( 1, maxval );
    	var lval;
    	if (bia > 0)
    	{
    		lval = log( r ) / maxilog * maxval;
    		r = lval * bia + r * (1 - bia);
    	}
    	if (bia < 0)
    	{
    		lval = log( r ) / maxilog * maxval;
    		r = maxval - 1 + lval * bia - r * (1 + bia);
    	}
    	r = mi + (ma - mi) * r / maxval;
    	return r;
    }
    Last edited by Alexx; 03-27-2013 at 03:58 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •