After I build a JS function that generates pseudo numbers from initial seed I created the same function in C# expecting to get the same results. After 6 iterations the results where different... Can someone help me to build such a function that generates same values in JS and also in C# ?
using System;
public class PSR
{
public int ITN { get; private set; } = 0;
public int IntITN { get; private set; } = 0;
private double seed;
public PSR(double seed)
{
this.seed = seed + 0.5; // avoid 0
}
public double Next()
{
ITN++;
var x = Math.Sin(this.seed) * 1000;
var result = x - Math.Floor(x); // [0.0,1.0)
this.seed = result; // for next call
return result;
}
public double NextInt(double lo, double hi)
{
IntITN++;
var x = this.Next();
return Math.Truncate((hi - lo) * x + lo);
}
}
TS version
export class Psr
{
itn: number = 0;
intItn:number = 0;
constructor(private seed) {
this.seed = seed + 0.5; // avoid 0
}
next() {
this.itn++
let x = Math.sin(this.seed) * 1000;
let result = x - Math.floor(x); // [0.0,1.0)
this.seed = result; // for next call
return result;
}
nextInt(lo, hi) {
this.intItn++
let x = this.next();
return Math.trunc((hi - lo) * x + lo);
}
}