Newsgroups: comp.theory,comp.ai.genetic,comp.ai
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!gatech!howland.reston.ans.net!Germany.EU.net!EU.net!sun4nl!news.nic.surfnet.nl!spider.research.ptt.nl!schelvis
From: schelvis@braga.research.ptt.nl (Wilbert Schelvis)
Subject: Re: wanted: FAST binary random number gene
Message-ID: <D5uECr.KLD@news.research.ptt.nl>
Sender: usenet@news.research.ptt.nl (USENET News System)
Nntp-Posting-Host: braga.research.ptt.nl
Organization: PTT Research, The Netherlands
References: <3k6tp7$t13@vixen.cso.uiuc.edu> <1995Mar16.135500.22980@news.unige.ch>
Date: Wed, 22 Mar 1995 12:50:50 GMT
Lines: 52
Xref: glinda.oz.cs.cmu.edu comp.theory:12039 comp.ai.genetic:5377 comp.ai:28361

In article <1995Mar16.135500.22980@news.unige.ch>,
Silvere Martin-Michiellot <sylvere@divsun.unige.ch> wrote:
>In article t13@vixen.cso.uiuc.edu, dputnam@prairienet.org (Dan Putnam) writes:
>>Byung-Ro Moon (moon@maui.cs.ucla.edu) wrote:
>>: I'm looking for a very FAST and reasonable binary random number generator.
>>: As I need fairly many calls for this, random()%2 (or random() mod 2)
>>: takes too much time as the Unix routine random() or rand() are not so cheap.


A good method for generating semi-random bit sequences by using
primitive polynomes if GF(2), well known in coding theory. I will not
bother you with the details, but give a simple algorithm.


main()
{
    int reg = 5;  /* can be any value except 0 */

    while(1)
    {
	reg <<= 1;
	reg += (((reg & 0x2000) == 0) == ((reg & 0x0800) == 0));

        /* least significant bit of reg is pseudo-random */		

	printf("%s", (reg & 0x01) ? "1" : "0");
    }
}


This algorithm will generate a pseudo-random bit string, with a period
of 2^13 - 1. Longer sequences can also be generated by using long
integers and replacing the values 0x2000 and 0x0800 by

	0x00020000 and 0x00008000 (sequence length 2^17 - 1) or
	0x00080000 and 0x00020000 (sequence length 2^19 - 1) or
	0x00800000 and 0x00200000 (sequence length 2^23 - 1) or
	0x20000000 and 0x08000000 (sequence length 2^29 - 1) or
	0x80000000 and 0x20000000 (sequence length 2^31 - 1) 

respectively.

Gr., Wilbert

--------------------------------------------------------------------------

Wilbert Schelvis                     Royal PTT Nederland
KPN Research, Neher Laboratories     room:  LE124
PO BOX 421                           phone: +31703326486
2260 AK  Leidschendam                fax:   +31703326477
the Netherlands                      email: w.a.m.schelvis@research.ptt.nl

