Article View: att.lang.c++
Article #19Re: random number generator
From: Jeff
Date: Tue, 03 Jun 2003 17:17
Date: Tue, 03 Jun 2003 17:17
78 lines
3209 bytes
3209 bytes
Marco de Boer wrote:
> Hi Lovens Weche,
>
> Just another simple lotto generator (balls out of 45) and sorted
>
> Good luck, Marco
>
> /*
> * Lotto Generator
> */
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> #define RAND(x) ((size_t)((x)*rand()/(1.0+RAND_MAX)))
> #define SWAP(t,a,b) {t _Swap=a;a=b;b=_Swap;}
>
> static void Lotto(size_t*,size_t,size_t);
> static int Compare(const void*,const void*);
>
> int main()
> {
> size_t *Array,iDrawing,iCount,iTotal;
>
> srand((unsigned int)time(NULL));
> iCount=6;
> iTotal=45;
> if ( (Array=calloc(iTotal,sizeof(size_t)))==NULL ) return 1;
> for ( iDrawing=0; iDrawing<20; iDrawing++ ) Lotto(Array,iCount,iTotal);
> free(Array);
> return 0;
> }
>
> static void Lotto(size_t *Array,size_t iCount,size_t iTotal)
> {
> size_t i,j;
>
> for ( i=0; i<iTotal; i++ ) Array[i]=i+1;
> for ( i=0; i<iCount; i++ ) {
> j=i+RAND(iTotal-i);
> SWAP(size_t,Array[i],Array[j]);
> }
> qsort(Array,iCount,sizeof(size_t),Compare);
> for ( i=0; i<iCount; i++ )
> (void)fprintf(stdout,"%2d%c",Array[i],(i+1==iCount)?'\n':' ');
> return;
> }
>
> static int Compare(const void *a,const void *b)
> {
> return (*(size_t**)a<*(size_t**)b)?-1:1;
> }
>
>
I suppose this combination has as much chance as any of occuring:
Hardy: $ ./lotto
1 2 3 4 5 6
1 2 3 4 6 7
1 2 3 4 5 7
1 2 4 5 6 7
1 2 3 4 5 6
1 2 4 5 6 7
1 2 3 4 5 6
1 2 3 4 6 7
1 2 3 4 5 6
1 2 3 4 6 7
1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 5 7
1 2 3 4 5 7
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 7
1 2 3 4 5 6
1 2 3 4 5 7
Technical Details
Message-ID:
<xK4Da.37164$da1.7490@nwrddc03.gnilink.net>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!news-xfer.cox.net!peer02.cox.net!peer01.cox.net!cox.net!cyclone1.gnilink.net!spamkiller.gnilink.net!nwrddc03.gnilink.net.POSTED!53ab2750!not-for-mailReferences:
<SikCa.1506$Z83.788@wagner.videotron.net> <bbdhoj$n67$1@news1.tilbu1.nb.home.nl>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d4ba,da54b5a763359bc3 X-Google-Attributes: gid10d4ba,public X-Google-Thread: 109fba,da54b5a763359bc3 X-Google-Attributes: gid109fba,public X-Google-Thread: 1014db,da54b5a763359bc3 X-Google-Attributes: gid1014db,public X-Google-ArrivalTime: 2003-06-03 10:17:17 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!news-xfer.cox.net!peer02.cox.net!peer01.cox.net!cox.net!cyclone1.gnilink.net!spamkiller.gnilink.net!nwrddc03.gnilink.net.POSTED!53ab2750!not-for-mail From: Jeff <jeff@whitehouse.gov> User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.3) Gecko/20030312 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: att.lang.c++,comp.lang.c,comp.lang.c++ Subject: Re: random number generator References: <SikCa.1506$Z83.788@wagner.videotron.net> <bbdhoj$n67$1@news1.tilbu1.nb.home.nl> In-Reply-To: <bbdhoj$n67$1@news1.tilbu1.nb.home.nl> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Lines: 81 Message-ID: <xK4Da.37164$da1.7490@nwrddc03.gnilink.net> Date: Tue, 03 Jun 2003 17:17:17 GMT NNTP-Posting-Host: 151.205.193.186 X-Complaints-To: abuse@verizon.net X-Trace: nwrddc03.gnilink.net 1054660637 151.205.193.186 (Tue, 03 Jun 2003 13:17:17 EDT) NNTP-Posting-Date: Tue, 03 Jun 2003 13:17:17 EDT