Re: lottery number thing problem

From: Chris Dams (chrisd_at_gamow.sci.kun.nl)
Date: 11/12/03


Date: Wed, 12 Nov 2003 10:29:54 +0000 (UTC)

Hello,

"moi" <random1234_NOSPAM_@blueyonder.co.uk> writes:

>p.s. i am aware from some replies to earlier posts that some of the things
>like the includes' etc is a bit archaic but for the purposes of this
>assignment, i dont really need it pointing out. cheers.

So, if you are aware of that, why don't you change it? It looks a bit silly.

>bool checknum(int num, int lottery_numbers[5])
>{
> for (int x=0; x <= 5; x++)
> {
> //check number is not equal to any other number already in array
> //and is greater than zero.
> if (lottery_numbers[x] == num || lottery_numbers[x] == 0)

Why test if the number ==0? Why not generate numbers >0 to begin with?

> {
> return false; //the number is invalid. it is rejected.
> }
> }
> return true; //the number is valid. it is accepted
>}

>void DrawNumbers(int lottery_numbers[5])
>{
>int num_count = 0; //counter for number of valid numbers found.

> while (num_count <=5) //i.e. while all 6 numbers have not yet been drawn.
> {
> //initialise random number generator
> time_t t;
> srand(unsigned (time(&t)));

The srand should not be inside the loop. Move it out of it.

> int num = (rand()*49); //generate a random number between 0 and 49

This generates all kinds of things, except what you want. Think about it.
The rand() function returns a pseudo-random integer between 0 and RAND_MAX.
How would you make an integer between >0 and <= 49 from that with (almost)
equal probability? Or, if you like, try to figure out how to make the
probabilities exactly equal.

>void main(int lottery_numbers[5])

main should return an int. Furthermore why does it take in integer array
as an argument. What is wrong with int main()?

>{

Declare your lottery_numbers inside main. Like
        int lottery_numbers[5]={-1,-1,-1,-1,-1}; .
Initialize with -1 to prevent discarding numbers because some random
memory location happened to already contain a valid lottery number.

Bye,
Chris Dams



Relevant Pages

  • Re: lottery number thing problem
    ... i dont really need it pointing out. ... The randfunction returns a pseudo-random integer between 0 and RAND_MAX. ... What is wrong with int main? ...
    (alt.comp.lang.learn.c-cpp)
  • Re: SmallC
    ... INT 74h is the interrupt service (ISR) for PS/2-compatible pointing ... Do ypu have e full description of the INT 74h? ... replies, sys cmmds to keyboard, keyboard replies to system, sys cmmds ...
    (alt.lang.asm)
  • Re: SmallC
    ... INT 74h is the interrupt service for PS/2-compatible pointing ... may be masked by setting bit 4 on I/O port A1h ... mov ax, 0C205h ...
    (alt.lang.asm)
  • Re: SmallC
    ... INT 74h is the interrupt service for PS/2-compatible pointing ... MS/PC-DOS use the IBM Interrupt Sharing ... The ISR collects each byte of the packet and stores ...
    (alt.lang.asm)
  • Re: SmallC
    ... INT 74h is the interrupt service for PS/2-compatible pointing ... MS/PC-DOS use the IBM Interrupt Sharing ... The ISR collects each byte of the packet and stores ...
    (alt.lang.asm)