🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: att.lang.c++
Article #13

Re: random number generator

#13
From: Marco de Boer
Date: Sun, 01 Jun 2003 18:47
52 lines
2510 bytes
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;
}
Technical Details
Message-ID: <bbdhoj$n67$1@news1.tilbu1.nb.home.nl>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!newsfeed2.earthlink.net!newsfeed.earthlink.net!newsfeed.news2me.com!skynet.be!skynet.be!213.51.129.3.MISMATCH!newshub1.home.nl!home.nl!not-for-mail
References: <SikCa.1506$Z83.788@wagner.videotron.net>

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-01 11:47:47 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!newsfeed2.earthlink.net!newsfeed.earthlink.net!newsfeed.news2me.com!skynet.be!skynet.be!213.51.129.3.MISMATCH!newshub1.home.nl!home.nl!not-for-mail
From: "Marco de Boer" <marco_de_boer@hotmail.com>
Newsgroups: att.lang.c++,comp.lang.c,comp.lang.c++
Subject: Re: random number generator
Date: Sun, 1 Jun 2003 20:47:44 +0200
Organization: @Home Benelux
Lines: 54
Message-ID: <bbdhoj$n67$1@news1.tilbu1.nb.home.nl>
References: <SikCa.1506$Z83.788@wagner.videotron.net>
NNTP-Posting-Host: co267668-a.almel1.ov.home.nl
X-Trace: news1.tilbu1.nb.home.nl 1054493267 23751 213.51.239.114 (1 Jun 2003 18:47:47 GMT)
X-Complaints-To: abuse@home.nl
NNTP-Posting-Date: Sun, 1 Jun 2003 18:47:47 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165