From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 109d8a,3cb5ab44ff758e6b X-Google-NewGroupId: yes X-Google-Thread: 1014db,3cb5ab44ff758e6b X-Google-NewGroupId: yes X-Google-Thread: fb57f,3cb5ab44ff758e6b X-Google-NewGroupId: yes X-Google-Thread: 101deb,43a0a81c48fef482,start X-Google-NewGroupId: yes X-Google-Thread: 1094ba,43a0a81c48fef482,start X-Google-NewGroupId: yes X-Google-Thread: 103376,43a0a81c48fef482,start X-Google-NewGroupId: yes X-Google-Thread: 107079,43a0a81c48fef482,start X-Google-NewGroupId: yes X-Google-Attributes: gid9ef9b79ae9,gid4516fb5702,gid2dda566af5,gidbda4de328f,gid8d3408f8c3,gida07f3367d7,gid7a498f3897,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!feeder.news-service.com!goblin3!goblin.stu.neva.ru!exi-transit.telstra.net!news.telstra.net!exi-spool.telstra.net!exi-reader.telstra.net!not-for-mail From: "robin" Newsgroups: sci.math,comp.lang.c,sci.crypt,comp.lang.pl1,comp.lang.fortran,comp.lang.ada,sci.math.num-analysis References: <603ebe15-a32f-4fbb-ba44-6c73f7919a33@t35g2000yqj.googlegroups.com> Subject: Re: RNGs with periods exceeding 10^(40million). Date: Tue, 18 Jan 2011 03:10:05 +1100 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5994 Message-ID: <4d346a5a$0$66039$c30e37c6@exi-reader.telstra.net> NNTP-Posting-Host: 123.3.24.17 X-Trace: 1295280732 exi-reader.telstra.net 66039 123.3.24.17:1038 Xref: g2news2.google.com sci.math:217684 comp.lang.c:122313 sci.crypt:30786 comp.lang.pl1:2082 comp.lang.fortran:34260 comp.lang.ada:17462 sci.math.num-analysis:17375 Date: 2011-01-18T03:10:05+11:00 List-Id: /*** 32-bit version in PL/I ***/ (nosize): rng: procedure options (main); declare Q(0:4194303) unsigned fixed binary (32), carry unsigned fixed binary (32) static initial (0); b32MWC: procedure () returns (unsigned fixed binary (32)); declare (t,x) unsigned fixed binary (32); declare j fixed binary (31) static initial (4194303); j=iand(j+1, 4194303); x=Q(j); t=isll(x, 28)+carry; carry=isrl(x, 4)-(t wrote in message news:603ebe15-a32f-4fbb-ba44-6c73f7919a33@t35g2000yqj.googlegroups.com... | | I offer here a MWC (Multiply-With-Carry) RNG whose | period I cannot determine---nor is it likely that | anyone else can---but that unknown period is almost | certainly greater than 10^40000000, i.e, 10^(40million). | Versions for either 32- and 64-bit integers are given, | as well as suggestions for similar versions with | periods as great or even greater magnitude. | | MWC RNGs are ordinarily based on primes of the form | p=ab^r-1, with b=2^32 or 2^64 and a10^40403570, but for primes q, the average value of | order(2,q)/q is around .572. Thus, even if m has, say, | 40 prime factors, since (.572)^40>10^(-10), we can only | expect to "lose" around ten 10's, and perhaps ten more | through common factors for the lcm, providing an estimate | around 10^40403550 for the order of 2 mod m. | | That the order of 2 mod m is less than 10^(40million) | seems extremely unlikely; we would have to "lose" over | 400thousand 10's, when something in the range | of three to sixty 10's seems more likely. | | So users should feel confident that the periods of the | following MWC RNGs far exceed 10^(40million). Based on | m=(2^28-1)b^(2^22)-1=(2^28-1)b^4194304-1 with b=2^32, | m=(2^28-1)B^(2^21)-1=(2^28-1)B^2091752-1 with B=2^64, | they are simple and very fast. | You would have to observe more than 134 million | successive bits produced be these MWC RNGs | in order to get close to cracking them. | | Note that choosing a=2^i-1 in m=ab^r-1 makes finding | the top and bottom 32 bits of a purported 64 bit | a*x+c feasible using only 32-bit arithmetic, | or, within 64-bit arithmetic, finding the top | and bottom 64 bits of a purported 128 bit a*x+c. | | Here are C versions using, for 32-bit integers, | an unsigned long array Q[41943104], and for 64-bits, | an unsigned long long array Q[2091752]. | Try them and see if you get the same results I do. | | --------------------32-bit MWC version------------------- | | #include | static unsigned long Q[4194304],carry=0; | | unsigned long b32MWC(void) | {unsigned long t,x; static int j=4194303; | j=(j+1)&4194303; | x=Q[j]; t=(x<<28)+carry; | carry=(x>>4)-(t>17), xs^=(xs<<5) ) | #define KISS ( b32MWC()+CNG+XS ) | | int main(void) | {unsigned long int i,x,cng=123456789,xs=362436069; | /* First seed Q[] with CNG+XS: */ | for(i=0;i<4194304;i++) Q[i]=CNG+XS; | /* Then generate 10^9 b32MWC()s */ | for(i=0;i<1000000000;i++) x=b32MWC(); | printf("Does x=2769813733 ?\n x=%lu\n",x); | /* followed by 10^9 KISSes: */ | for(i=0;i<1000000000;i++) x=KISS; | printf("Does x=3545999299 ?\n x=%lu\n",x); | return 0; | } | | ---------------- 64-bit MWC version --------------------- | | #include | static unsigned long long Q[2097152],carry=0; | | unsigned long long B64MWC(void) | {unsigned long long t,x; static int j=2097151; | j=(j+1)&2097151; | x=Q[j]; t=(x<<28)+carry; | carry=(x>>36)-(t>17), xs^=(xs<<43) ) | #define KISS ( B64MWC()+CNG+XS ) | | int main(void) | {unsigned long long i,x,cng=123456789987654321LL, | xs=362436069362436069LL; | /* First seed Q[] with CNG+XS: */ | for(i=0;i<2097152;i++) Q[i]=CNG+XS; | /* Then generate 10^9 B64MWC()s */ | for(i=0;i<1000000000;i++) x=B64MWC(); | printf("Does x=13596816608992115578 ?\n x=%llu\n",x); | /* followed by 10^9 KISSes: */ | for(i=0;i<1000000000;i++) x=KISS; | printf("Does x=5033346742750153761 ?\n x=%llu\n",x); | return 0; | } | | --------------------------------------------------------- | | Note that I advocate the KISS, (Keep-It-Simple-Stupid), | principle. Even though the MWC RNGs perform very well on | tests of randomness, combining with Congruential (CNG) | and Xorshift (XS) probably provides extra insurance | at the cost of a few simple instructions, (and making | the resulting KISS even harder to crack). | | Also note that for unsigned integers such as in Fortran, | the C instruction -(t1, | the period is still likely to exceed 10^(40million). | There are two exceptions: | All Q[i]=0, carry=0 yields j=0 and the binary expansion | 0/m=.00000000000000000... | All Q[i]=b-1=2^32-1 and carry=a-1=(2^28-2) yields j=m | and the binary expansion | m/m=.11111111111111111... | | Almost any choice of m=(2^i-1)*2^(2^27)-1 is likely | to provide an immense order for 2 mod m. | Choices i=16,18,22,28 resulted in m's that have no | factors<10^7. Memory seems to be the key restriction | for the resulting Q[] array. With enough memory, | one might seek i's for which m=(2^i-1)*2^(2^28)-1 | has no small factors, or even m=(2^i-1)*2^(2^29)-1, | boosting the unknown and unknowable periods of | MWC RNGs to ranges beyond 10^(40million). | | George Marsaglia