Article View: comp.programming.contests
Article #3Re: Random Numbers
From: Chris Wilkins
Date: Mon, 30 Oct 1995 23:59
Date: Mon, 30 Oct 1995 23:59
28 lines
1582 bytes
1582 bytes
In article <4745p3$62h@umbc9.umbc.edu> schlein@umbc.edu (Jonas J. Schlein) writes:
>>> I have 13 array locations. Let's call them: n[0], n[1], n[2],....,
>>> n[12]. I want to RANDOMLY fill those locations with integers
>>> from 0 to 12.
>> Why not do something like this:
>>
>> for(i = 0; i < 13; i++)
>> n[i] = i;
>>
>> for(i = 0; i < 13; i++) {
>> s = random_number_from_0_to_12();
>> swap(n, i, s);
>> }
>Actually if you go strictly by what my Algorithms Professor says is a
>better way (he's a crypto man so I won't argue with him on that) for a
>random distribution would be to replace your random_number_from_0_to_12()
>with random_number_from_0_to_i(). Also you can then start the second
>for loop at 1 to avoid the unnecessary initial sway of n[0] with itself.
This sounds seriously wrong. I think what you meant is that you count DOWN
through the array swapping each element with one of the remaining elements or
itself (with equal probabilities). All this was covered in the shuffling
algorithm thread a couple of days ago.
Chris
Technical Details
Message-ID:
<chrisw.282.0017FC83@leland.stanford.edu>Path:
arc1-ca.pugleaf.net!burble.Stanford.EDU!chriswReferences:
<46qte7$hbv@btmpjg.god.bel.alcatel.be> <46r395$2tp@fido.asd.sgi.com> <4745p3$62h@umbc9.umbc.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!burble.Stanford.EDU!chrisw From: chrisw@leland.stanford.edu (Chris Wilkins) Newsgroups: comp.programming.contests,comp.lang.c Subject: Re: Random Numbers Date: Mon, 30 Oct 1995 23:59:01 Organization: Stanford University Message-ID: <chrisw.282.0017FC83@leland.stanford.edu> References: <46qte7$hbv@btmpjg.god.bel.alcatel.be> <46r395$2tp@fido.asd.sgi.com> <4745p3$62h@umbc9.umbc.edu> NNTP-Posting-Host: burble.stanford.edu X-Newsreader: Trumpet for Windows [Version 1.0 Rev A]