🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.programming.contests
Article #2

Re: Random Numbers

#2
From: Jonas J. Schlein
Date: Tue, 31 Oct 1995 03:39
23 lines
1250 bytes
>> 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.
-- 
"If it wasn't for C, we would be using BASI, PASAL, and OBOL."

Jonas J. Schlein  (schlein@gl.umbc.edu)
Technical Details
Message-ID: <4745p3$62h@umbc9.umbc.edu>
Path: arc1-ca.pugleaf.net!news.umbc.edu!not-for-mail
References: <46qte7$hbv@btmpjg.god.bel.alcatel.be> <46r395$2tp@fido.asd.sgi.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.umbc.edu!not-for-mail
From: schlein@umbc.edu (Jonas J. Schlein)
Newsgroups: comp.programming.contests,comp.lang.c
Subject: Re: Random Numbers
Date: 30 Oct 1995 22:39:15 -0500
Organization: University of Maryland Baltimore County
Message-ID: <4745p3$62h@umbc9.umbc.edu>
References: <46qte7$hbv@btmpjg.god.bel.alcatel.be> <46r395$2tp@fido.asd.sgi.com>
NNTP-Posting-Host: f-umbc9.umbc.edu
NNTP-Posting-User: schlein