Decrypted
Decrypted
CC#
Created by Vlad on 4/2/2024 in #help
Feeding Random into itself - does it "corrupt the randomness"?
public class Rng
{
public byte[] State => _state;

private byte[] _state = new byte[32];

public Rng(int seed)
{
var rand = new Random(seed);

// fill state from seed
rand.NextBytes(_state);
}

public Rng(byte[] state)
{
_state = state;
}

public int Next()
{
// Hash new state
if (!SHA256.TryHashData(_state, _state, out _))
{
throw new InvalidOperationException();
}

var bigInt = new BigInteger(_state);

return (int)(bigInt % int.MaxValue);
}
}
public class Rng
{
public byte[] State => _state;

private byte[] _state = new byte[32];

public Rng(int seed)
{
var rand = new Random(seed);

// fill state from seed
rand.NextBytes(_state);
}

public Rng(byte[] state)
{
_state = state;
}

public int Next()
{
// Hash new state
if (!SHA256.TryHashData(_state, _state, out _))
{
throw new InvalidOperationException();
}

var bigInt = new BigInteger(_state);

return (int)(bigInt % int.MaxValue);
}
}
This uses SHA256 to hash the state on top of itself You can then persist the 32 byte state and resume from it when required. Not sure if this is the best way to do this. I don't even know if the bigInt % int.MaxValue is safe operation for converting to int..
61 replies
CC#
Created by Vlad on 4/2/2024 in #help
Feeding Random into itself - does it "corrupt the randomness"?
I got resumable rand class somewhere, let me try to find it
61 replies