Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 92971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:16:16+00:00 2026-05-10T23:16:16+00:00

George Marsaglia has written an excellent random number generator that is extremely fast, simple,

  • 0

George Marsaglia has written an excellent random number generator that is extremely fast, simple, and has a much higher period than the Mersenne Twister. Here is the code with a description:

good C random number generator

I wanted to port the CMWC4096 code to Java, but it uses several unsigned datatypes so I am not sure how to do this properly. Here is the full C code:

/* choose random initial c<809430660 and */ /* 4096 random 32-bit integers for Q[]   */ static unsigned long Q[4096],c=362436;  unsigned long CMWC4096(void) {     unsigned long long t, a=18782LL;     static unsigned long i=4095;     unsigned long x,r=0xfffffffe;     i = (i+1) & 4095;     t = a*Q[i] + c;     c = (t>>32);     x = t + c;     if (x < c) {         x++;         c++;     }     return (Q[i] = r - x); } 

Can anyone port this to Java? How does this work when you only have signed numbers available?

EDIT: Thanks everybody for the quick answers! For the first 100 million numbers this java code seems to produce the same result as the C code. It is 3 times faster than Java’s java.util.Random.

public class ComplimentaryMultiplyWithCarryRandom {      /**      * Choose 4096 random 32-bit integers      */     private long[] Q;      /**      * choose random initial c<809430660      */     private long c = 362436;      private int i;      public ComplimentaryMultiplyWithCarryRandom() {         Random r = new Random(1);         Q = new long[4096];          // TODO initialize with real random 32bit values         for (int i = 0; i < 4096; ++i) {             long v = r.nextInt();             v -= Integer.MIN_VALUE;             Q[i] = v;         }         i = 4095;     }      int next() {         i = (i + 1) & 4095;         long t = 18782 * Q[i] + c;         c = t >>> 32;         long x = (t + c) & 0xffffffffL;         if (x < c) {             ++x;             ++c;         }          long v = 0xfffffffeL - x;         Q[i] = v;         return (int) v;     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:16:16+00:00Added an answer on May 10, 2026 at 11:16 pm

    Can anyone port this to Java? How does this work when you only have signed numbers available?

    No Stress! a=18782 so the largest t could ever be is not large enough to cause signed vs. unsigned problems. You would have to ‘upgrade’ the result of using Q to a value equal to a 32-bit unsigned number before using it anywhere. e.g. if Q is an int (32-bit signed) then you’d have to do this before using it in the t=a*Q[i]+c statement, e.g.

    t=a*(((long)Q[i])&0xffffffffL)+c 

    where this (((long)Q[i])&0xffffffffL) business promotes Q[i] to a 64-bit # and ensures its high 32 bits are 0’s. (edit: NOTE: you need 0xffffffffL here. Java does the wrong thing if you use 0xffffffff, it seems like it ‘optimizes’ itself to the wrong answer & you get a negative number if Q[i]’s high bit is 1.)

    You should be able to verify this by running the algorithms in C++ and Java to compare the outputs.

    edit: here’s a shot at it. I tried running it in C++ and Java for N=100000; they both match. Apologies if I used bad Java idioms, I’m still fairly new to Java.

    C++:

    // marsaglia2003.cpp   #include <stdio.h> #include <stdlib.h> // for atoi  class m2003 {     enum {c0=362436, sz=4096, mask=4095};     unsigned long Q[sz];     unsigned long c;     short i;  public:     m2003()     {         // a real program would seed this with a good random seed         // i'm just putting in something that makes the output interesting         for (int j = 0; j < sz; ++j)             Q[j] = j + (j << 16);         i = 4095;         c = c0;     }      unsigned long next()     {         unsigned long long t, a=18782LL;         unsigned long x;         unsigned long r=0xfffffffe;         i = (i+1)&mask;         t=a*Q[i]+c;         c=(unsigned long)(t>>32);         x=(unsigned long)t + c;         if (x<c)         {             x++;             c++;         }         return (Q[i]=r-x);     } };  int main(int argc, char *argv[]) {     m2003 generator;     int n = 100;     if (argc > 1)         n = atoi(argv[1]);      for (int i = 0; i < n; ++i)     {         printf('%08x\n', generator.next());     }     return 0; } 

    java: (slower than compiled C++ but it matches for N=100000)

    // Marsaglia2003.java  import java.util.*;  class Marsaglia2003 {     final static private int sz=4096;     final static private int mask=4095;     final private int[] Q = new int[sz];     private int c=362436;     private int i=sz-1;      public Marsaglia2003()     {         // a real program would seed this with a good random seed         // i'm just putting in something that makes the output interesting         for (int j = 0; j < sz; ++j)             Q[j] = j + (j << 16);     }    public int next()      // note: returns a SIGNED 32-bit number.     // if you want to use as unsigned, cast to a (long),      // then AND it with 0xffffffffL     {         long t, a=18782;         int x;         int r=0xfffffffe;         i = (i+1)&mask;         long Qi = ((long)Q[i]) & 0xffffffffL; // treat as unsigned 32-bit         t=a*Qi+c;         c=(int)(t>>32);             // because 'a' is relatively small this result is also small          x=((int)t) + c;         if (x<c && x>=0) // tweak to treat x as unsigned         {             x++;             c++;         }         return (Q[i]=r-x);     }      public static void main(String args[])     {         Marsaglia2003 m2003 = new Marsaglia2003();          int n = 100;         if (args.length > 0)             n = Integer.parseInt(args[0]);         for (int i = 0; i < n; ++i)         {             System.out.printf('%08x\n', m2003.next());         }     } }; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an NSMutableArray called george. It has NSInteger values inside: 12, 23, 54,
I have legacy C++ code that I wrote to generate uniform random numbers and
I'm trying to implement George Marsaglia's Complementary Multiply-With-Carry algorithm in C. It seems to
I want to execute a javascript that returns a string like 'GEORGE SMITH'. I
table { id: long name: string } 1235 Fred 1902 Trever 5123 George 6467
I have some search queries like so: George AND NOT Washington OR Abraham Dog
Variable $name (string) gives something like (possible values): Elton John 2012 George Bush Julia
Variable $name (string) gives something like (5 possible values): Elton John Barak Obama George
Seatgeek has a zoomable draggable tiled interface. An example is here: http://seatgeek.com/sf-bulls-yankees-tickets/3-2-2012-tampa-george-steinbrenner-field/mlb/785875/# I want
I need to create a Linked List from a text file that looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.