How do I replicate PHP's PRNG algorithm implementation in C#? -
i've created procedural image generator uses default pseudo-random number generator built in php5.
i can set seed mt_srand($id);
, same numbers sequence (with mt_rand(0,255);
).
what need:
a prng implementation work exact same way in php , c#
example:
php:
mt_srand(123); echo mt_rand(0,255); //returns 34 echo mt_rand(0,255); //returns 102 echo mt_rand(0,255); //returns 57 echo mt_rand(0,255); //returns 212 echo mt_rand(0,255); //returns 183
c#:
setseed(123); print getrand(0,255); //returns 34 print getrand(0,255); //returns 102 print getrand(0,255); //returns 57 print getrand(0,255); //returns 212 print getrand(0,255); //returns 183
( ^ function names not referring existing ones, named example's sake )
thanks tips!
i resolved problem implementing custom prng algorithm myself both in c# , php.
since needed , didn't have time go through whole mersenne twister theory , 2 languages incompatibilities (like different behaviors of types , operators...) decided write simple prng algorigthm:
c#
code:
using system; public class ciaccorandom { private static int tree=0; public void plantseed(int seed) { tree = math.abs(seed) % 9999999+1; getrand(0, 9999999); } public int getrand(int min, int max) { tree = (tree*125)%2796203; return tree%(max-min+1)+min; } }
usage:
int seed = 123; int min = 0; int max = 255; ciaccorandom randomer = new ciaccorandom(); randomer.plantseed(seed); randomer.getrand(min, max); // returns pseudo-random int
php
code:
namespace ciacco_twister; class ciaccorandom { private static $tree = 0; public static function plantseed($seed) { self::$tree = abs(intval($seed)) % 9999999+1; self::getrand(); } public static function getrand($min = 0, $max = 9999999) { self::$tree = (self::$tree * 125) % 2796203; return self::$tree % ($max - $min + 1) + $min; } }
usage:
require_once "ciacco_twister.php"; use ciacco_twister\ciaccorandom; $seed = 123; $min = 0; $max = 255; ciaccorandom::seed($seed); ciaccorandom::getrand($min,$max); // returns pseudo-random int
notes:
i needed prng that, given seed , int range return same sequence of int numbers both in php , c#.
it's pretty limited works purpose!
maybe useful else...
Comments
Post a Comment