Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.


Math Forum
»
Discussions
»
sci.math.*
»
sci.math
Notice: We are no longer accepting new posts, but the forums will continue to be readable.
Topic:
RNGs: A Super KISS
Replies:
14
Last Post:
Feb 26, 2013 10:53 AM




Re: RNGs: A Super KISS
Posted:
Nov 4, 2009 4:31 PM


On Nov 4, 1:26 pm, user923005 <dcor...@connx.com> wrote: > On Nov 4, 9:18 am, David <david.astgt...@gmail.com> wrote: > > > On an x8664 machine using GCC version 4.3.3 (Ubuntu 4.3.35ubuntu4), > > both the C code and C++ code fail for me. > > I get: > > x=505478909. > > Does x=872412446? > > > Changing the unsigned long's to unsigned int's fixed the problem. > > And it does matter: before the change, the generator failed a variety > > of tests (really odd assortment, though: parking lot, 2dsphere, > > 3dsphere, squeeze, and sums). > > OK, makes sense. The RNG must assume 32 bit longs.
Modified C++ code:
#include <iostream>
class SuperKiss {
private: unsigned int Q[41790]; unsigned int indx; unsigned int carry; unsigned int xcng; unsigned int xs;
int refill () { int i; unsigned long long t; for (i = 0; i < 41790; i++) { t = 7010176LL * Q[i] + carry; carry = (t >> 32); Q[i] =(unsigned int) ~(t); } indx = 1; return (Q[0]); }
public: // Constructor: SuperKiss() { indx = 41790; carry = 362436; xcng = 1236789; xs = 521288629; unsigned i; for (i = 0; i < 41790; i++) Q[i] = (xcng = 69609 * xcng + 123) + (xs ^= xs << 13, xs ^= (unsigned) xs >> 17, xs ^= xs >> 5); }
// Collect next random number: unsigned int SKRand() { return (indx < 41790 ? Q[indx++] : refill ()) + (xcng = 69609 * xcng + 123) + (xs ^= xs << 13, xs ^= (unsigned) xs >> 17, xs ^= xs >> 5); } };
int main () { unsigned int i; int x=0; SuperKiss sk; for (i = 0; i < 1000000000; i++) x = sk.SKRand(); std::cout << " x = " << x << std::endl << "Does x=872412446?" << std::endl; return 0; }
/* Possible output: x = 872412446 Does x=872412446? */



