I dont know if anyone out there has ever had the need or has used a 64-bit random number generator.
I've seen some implementations out there (& I've ported several over from C & fortran), but none that have *really* suited my needs (most have a max parameter in the Next function but not a minimum). If anyone knows of a 64bit RNG class out there I'd appreciate a link or something to that nature. I'm not looking for a crypto-strength library (although is one is available I'd prefer that to a standard PRNG), but something that produces something usefull.
thanks
--MH

1 answers
You could just use the regular Random class like so:
Random rgen = new Random();
byte[] array = new byte[8];
rgen.NextBytes(array);
ulong num = 0;
for(int i = 0; i != 8; ++i) {
num <<= 8;
num |= array[i];
}
//num will be a random unsigned 64-bit integer
answered 2 years ago by:
0
that works, but I need to be able to specify a range (min, max) like you can with Next(int, int), next(long, long) and get a the 64bit int.
I'm sure I could implement min and max inside one implementation (a port of rp brent's c implementation of marsaglia's xorshift rngs), but I suck so bad at math that how and where are a guessing game for me.
here it is. do you where or how I could implement a min and max inside this?
<a target="_new" href="http://www.bindmaker.org/sw/Random64.cs">Here's a link if this looks too messy</a>
namespace NullFX.Util {
using System;
public class Random64 {
private const int wlen = 64;
private const int r = 64;
private const int s = 53;
private const int a = 33;
private const int b = 26;
private const int c = 27;
private const int d = 29;
private static ulong w;
private static ulong weil;
private static ulong[] x;
private ulong t;
private ulong v;
private static int i;
private int k;
private ulong _seed;
public Random64(long seed) {
this._seed = (ulong)seed;
weil = ((long)0x61c88646 << 32) + (long)0x80b583eb;
x = new ulong[r];
i = -1;
}
private long Next() {
// Initialisation necessary
if ((i < 0) || (this._seed != 0)) {
// v must be nonzero/
v = (this._seed != 0)?this._seed:~this._seed;
// Avoid correlations for close seeds
// This recurrence has period 2^64-1
for (k = wlen; k > 0; k--) {
v ^= (v ^= v << 7) >> 9;
}
// Initialise circular array
for (w = v, k = 0; k < r; k++) {
x[k] = (v ^= (v ^= v << 7) >> 9) + (w += weil);
}
// Discard first 4*r results (Gimeno)
for (i = r-1, k = 4*r; k > 0; k--) {
t = x[i = (i + 1) & (r - 1)];
v = x[(i + (r - s)) & (r - 1)];
t ^= (t ^= t << a) >> b;
v ^= v << c;
x[i] = (v ^= t^(v >> d));
}
}
// Increment i mod r (r is a power of 2)
t = x[i = (i + 1) & (r - 1)];
// Index is (i - s) mod r
v = x[(i + (r - s)) & (r - 1)];
// (I + L^a)(I + R^b)
t ^= (t ^= t<<a) >> b;
// I + L^c
v ^= v << c;
// Update circular array
x[i] = (v ^= t^(v>>d));
// Return combination with Weil generator
return (long)(v + (w += weil));
}
}
}
answered 2 years ago by:
2309
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!