Article View: be.comp.programming
Article #15580Re: hoe berekent een pc een "random" getal?
From: Koen Deforche
Date: Fri, 15 Sep 2000 00:00
Date: Fri, 15 Sep 2000 00:00
76 lines
3855 bytes
3855 bytes
ViperRT10 wrote:
>
> ik begrijp niet hoe een pc aan een willekeurig getal kan komen?
> je kan toch niet wiskundig een willekeurig getal berekenen? of gaat dat wel?
Nee, wiskundig gaat dat niet: het is niet echt toevalsmatig (i.e. je
kan 2 maal dezelfde random sequentie maken), maar voldoen wel aan een
aantal eigenschappen waaraan een toevalsmatig getal ook aan voldoet:
ze behoren tot een bepaalde distrubitie, waarbij men meestal vertrekt
van een uniform verdeelde distrubitie (daaruit kan je andere distrubities
afleiden).
Computers generen pseudo-random sequenties. Dit gebeurt meestal (altijd?)
door een of andere wiskundige formule die gebaseerd is op modulo rekenen.
Om er nog wat toevalsmatigheid aan toe te voegen, wordt de toestandsmachine
die op die manier pseudo-random sequenties genereert, ge-initialiseerd met
iets toevalsmatiger (zoals 'het aantal micro-seconden sinds 1970 module 1
seconde' bijvoorbeeld, of dat in combinatie met het process-id),
dat door de programmeur zelf gekozen wordt.
Of uit 'man drand48' (die trouwens alles piekfijn uitlegt), uit
de Linux Programmers Manual :
All the functions work by generating a sequence of 48-bit
integers, Xi, according to the linear congruential for
mula:
Xn+1 = (aXn + c) mod m, where n >= 0
The parameter m = 2^48, hence 48-bit integer arithmetic is
performed. Unless lcong48() is called, a and c are given
by:
a = 0x5DEECE66D
c = 0xB
The value returned by any of the functions drand48(),
erand48(), lrand48(), nrand48(), mrand48() or jrand48() is
computed by first generating the next 48-bit Xi in the
sequence. Then the appropriate number of bits, according
to the type of data item to be returned, is copied from
the high-order bits of Xi and transformed into the
returned value.
The functions drand48(), lrand48() and mrand48() store the
last 48-bit Xi generated in an internal buffer. The func
tions erand48(), nrand48() and jrand48() require the call
ing program to provide storage for the successive Xi val
ues in the array argument xsubi. The functions are ini
tialized by placing the initial value of Xi into the array
before calling the function for the first time.
The initializer function srand48() sets the high order
32-bits of Xi to the argument seedval. The low order
16-bits are set to the arbitrary value 0x330E.
btw.:
Random-number generation is a complex topic. The Numeri
cal Recipes in C book (see reference above) provides an
excellent discussion of practical random-number generation
issues in Chapter 7 (Random Numbers).
(of, God sta u bij, sla er 'The Art of Computer Programming'
van Knuth er op na)
koen.
Technical Details
Message-ID:
<39C26B3E.5EFD5EF6@chello.be>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<j6sw5.16325$R_2.161315@afrodite.telenet-ops.be>Show Full Headers
Complete Article Headers
X-Google-Language: DUTCH,ASCII X-Google-Thread: 107324,959acee4ea909053 X-Google-Attributes: gid107324,public X-Google-Thread: fcea4,959acee4ea909053 X-Google-Attributes: gidfcea4,public From: Koen Deforche <koen.deforche@chello.be> Subject: Re: hoe berekent een pc een "random" getal? Date: 2000/09/15 Message-ID: <39C26B3E.5EFD5EF6@chello.be> X-Deja-AN: 670243269 Content-Transfer-Encoding: quoted-printable References: <j6sw5.16325$R_2.161315@afrodite.telenet-ops.be> X-Accept-Language: en Content-Type: text/plain; charset=iso-8859-1 X-Complaints-To: abuse@chello.be X-Trace: news.chello.be 969042750 195.162.216.30 (Fri, 15 Sep 2000 20:32:30 MET DST) Organization: Chello Usenet Service MIME-Version: 1.0 NNTP-Posting-Date: Fri, 15 Sep 2000 20:32:30 MET DST Newsgroups: be.comp.programming,nl.comp.programmeren