Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > UK lottery number generator

Reply
Thread Tools

UK lottery number generator

 
 
Miktor
Guest
Posts: n/a
 
      04-14-2007
I'm trying to write a lottery number generator for the uk national
lottery.

Any clues where I'm going wrong?

#include <cstdlib>
#include <iostream>
using namespace std;

int main ()
{ // opening bracket of main() section
// declare the other variables we are going to use
int n=0;
int random_number=0;
int lottery_lines [10][7];
int number_bag [50];
int line_no=1;
int pos_no=1;
int selection;
int count=49;
int x=1;

// initialise contents of number_bag
// to contain numbers 1-49 in positions 1-49
for (n=1;n<=49;n++)
{
number_bag [n] = n;
cout << "Number_bag ";
cout << n;
cout << " = ";
cout << number_bag [n];
cout << "\n";
}

// BEGINNING OF LINE LOOP +++++++++++++++++++++++++++++++++++++++++++++
+++++++++
for (line_no=1; (line_no != 9) and (pos_no != 2); line_no++)
// break out of the line loop if we are at line 9
{ // opening bracket for line loop

// inner program loop
// generates numbers in positions 1 to 6 for each line

// BEGINNING OF POSITION LOOP
==================================================
for (pos_no=1;pos_no<=6;pos_no++)
{ // opening bracket for position loop
// assign selection a random number between 1 and (49-count)
int selection = 1 + (rand () % count);
lottery_lines [line_no] [pos_no] = number_bag [selection];
cout << "Line ";
cout << line_no;
cout << " , number ";
cout << pos_no;
cout << " = ";
cout << lottery_lines [line_no] [pos_no];
cout << "\n";
count--;

// remove used numbers from number_bag array
// by moving all the numbers from number_bag [selection] down
one
for (x=1; x<=(count-selection);x++)
{
cout << "\n";
cout << "Moving number_bag ";
cout << (selection+x);
cout << " down one position";
cout << "\n";
number_bag [selection+x] = number_bag [selection+x+1];
}

} // closing bracket for position loop
// END OF POSITION LOOP
================================================== ======

} // END OF LINE LOOP +++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++

// at this stage, 49 numbers have been drawn,
// so we need to place another 5 random numbers from 1-49
// in number_bag [1] to number_bag [5]

for (n=1;n<=5;n++)
{
number_bag [n] = 1 + (rand () % 49);
// force repeat selection if number_bag [n] = number_bag [1]
if (number_bag [n] = number_bag [1])
{
n--;
}
}

// test that none of the numbers
// in number_bag [2] to [6] are repeats of each other
// CODE HERE

// assign the values of lottery_lines [9][1] to [9][6]
// to number_bag [1] to [6]
for (n=1;n<=6;n++)
{
lottery_lines [9][n] = number_bag [n];
}

// output the nine lines of lottery numbers
for (line_no=1;line_no<=9;line_no++)
{

cout << "\n";
cout << "LINE NO.";
cout << line_no;
cout << "\t";

for (pos_no=1;pos_no<=6;pos_no++)
{
cout << lottery_lines [line_no][pos_no];
cout << "\t";
}

}

system("PAUSE");
return 0;
} // closing bracket of main() section

 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      04-14-2007
On 14 Apr 2007 12:27:01 -0700, "Miktor" <>
wrote in comp.lang.c++:

> I'm trying to write a lottery number generator for the uk national
> lottery.
>
> Any clues where I'm going wrong?


[snip code]

I don't know. You did one thing right, but one thing wrong.

The good thing that you did was to post your actual code. The thing
you did wrong was not to tell us what your problem with it is.

Do you get compiler or linker errors when you try to compile it? Does
it crash when you run it? Does it build and run OK, put produce what
you think are wrong results?

Post your code again but add some more information. Copy any
compiler, linker, or run time error messages and paste them into the
message. Or describe the incorrect output of the program and what you
think the correct output should be.

Help us to help you.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
 
 
 
Miktor
Guest
Posts: n/a
 
      04-14-2007
On Apr 14, 8:44 pm, Jack Klein <jackkl...@spamcop.net> wrote:
> On 14 Apr 2007 12:27:01 -0700, "Miktor" <bigbadmick2...@hotmail.com>
> wrote in comp.lang.c++:
>
> > I'm trying to write a lottery number generator for the uk national
> > lottery.

>
> > Any clues where I'm going wrong?

>
> [snip code]
>
> I don't know. You did one thing right, but one thing wrong.
>
> The good thing that you did was to post your actual code. The thing
> you did wrong was not to tell us what your problem with it is.
>
> Do you get compiler or linker errors when you try to compile it? Does
> it crash when you run it? Does it build and run OK, put produce what
> you think are wrong results?
>
> Post your code again but add some more information. Copy any
> compiler, linker, or run time error messages and paste them into the
> message. Or describe the incorrect output of the program and what you
> think the correct output should be.
>
> Help us to help you.
>
> --
> Jack Klein
> Home:http://JK-Technology.Com
> FAQs for
> comp.lang.chttp://c-faq.com/
> comp.lang.c++http://www.parashift.com/c++-faq-lite/
> alt.comp.lang.learn.c-c++http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


Thanks for the advice Jack.

When I compile and run the program in dev c++, it compiles and runs
ok, but it stops at line 8, number 6. I think it seems to be stuck in
an infinite loop or something.

If you cut and paste the above source code into dev c++, you'll see
what I mean.



Thanks again for the advice! I'm just trying to learn c++ from scratch
at the minute, with my entire previous programming experience
consisting of writing BASIC programs on my ZX Spectrum +2A many, many
years ago. Needless to say, it's an interesting learning curve!

 
Reply With Quote
 
Obnoxious User
Guest
Posts: n/a
 
      04-14-2007
Miktor skrev:
> I'm trying to write a lottery number generator for the uk national
> lottery.
>
> Any clues where I'm going wrong?
>


[snip]

>
> // at this stage, 49 numbers have been drawn,
> // so we need to place another 5 random numbers from 1-49
> // in number_bag [1] to number_bag [5]
>
> for (n=1;n<=5;n++)
> {
> number_bag [n] = 1 + (rand () % 49);
> // force repeat selection if number_bag [n] = number_bag [1]
> if (number_bag [n] = number_bag [1])


Just a guess, without reading the entire code:

if(number_bag[n] == number_bag[1])

> {
> n--;
> }
> }


[snip]

--
OU
 
Reply With Quote
 
livibetter
Guest
Posts: n/a
 
      04-14-2007
On Apr 15, 3:27 am, "Miktor" <bigbadmick2...@hotmail.com> wrote:
> I'm trying to write a lottery number generator for the uk national
> lottery.
>
> Any clues where I'm going wrong?
>


[snip]
> for (line_no=1; (line_no != 9) and (pos_no != 2); line_no++)


Are you sure your code can be compiled by C++ compiler? I don't know C+
+ has an "and" keyword. I think "and" should be &&.

And if dev c++ has debugger, have you tried to use it to debug?

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      04-14-2007
On 14 Apr 2007 13:27:37 -0700, "livibetter" <>
wrote in comp.lang.c++:

> On Apr 15, 3:27 am, "Miktor" <bigbadmick2...@hotmail.com> wrote:
> > I'm trying to write a lottery number generator for the uk national
> > lottery.
> >
> > Any clues where I'm going wrong?
> >

>
> [snip]
> > for (line_no=1; (line_no != 9) and (pos_no != 2); line_no++)

>
> Are you sure your code can be compiled by C++ compiler? I don't know C+
> + has an "and" keyword. I think "and" should be &&.


Yes indeed, C++ has the "and" keyword, as well as "and_eq", "bitand",
"bitor", "compl", "not", "not_eq", "or_eq", "xor", and "xor_eq".

They are all official keywords, and have been since the original
version of the ANSI/ISO C++ standard was adopted in 1998. Perhaps
even earlier, since before they were added to the C++ standard, they
were provided by macros in the C standard header <iso646.h> added to C
in 1995. Many C++ compilers provided that header once it became
standard C.

> And if dev c++ has debugger, have you tried to use it to debug?


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
Obnoxious User
Guest
Posts: n/a
 
      04-14-2007
Miktor skrev:
> I'm trying to write a lottery number generator for the uk national
> lottery.
>
> Any clues where I'm going wrong?


[snip]

>
> // at this stage, 49 numbers have been drawn,
> // so we need to place another 5 random numbers from 1-49
> // in number_bag [1] to number_bag [5]
>
> for (n=1;n<=5;n++)
> {
> number_bag [n] = 1 + (rand () % 49);
> // force repeat selection if number_bag [n] = number_bag [1]
> if (number_bag [n] = number_bag [1])
> {
> n--;
> }
> }


Here's another thing. You really should trace through your loop
and try to debug it. Consider:

First iteration with 'n' = 1.

number_bag[1] = /*random*/0;
if(number_bag[1] == number_bag[1]) {
--n; // 'n' equals 0
}
++n; // for loop increment, and so 'n' equals 1 again, again and again

--
OU
 
Reply With Quote
 
osmium
Guest
Posts: n/a
 
      04-14-2007
"Miktor" writes:

> I'm trying to write a lottery number generator for the uk national
> lottery.
>
> Any clues where I'm going wrong?


No clues. Where I live state governments have monopolies on the lottery,
both the mafia and the UK are locked out. "UK lottery" is an insufficient
program specification to allow one to debug.

You would save a certain amount of work, and add clarity, if you used the
random_shuffle() function in <algorithm>. One of my pet peeves:
random_shuffle() sounds more like it belongs in Cobol than a modern
programming language. ISTM that if something that puts things in order is
called "sort", then something that destroys order could simply be called
"shuffle"


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      04-16-2007

"osmium" <> wrote in message
news:...
> "Miktor" writes:
>
>. One of my pet peeves: random_shuffle() sounds more like it belongs in
>Cobol than a modern programming language. ISTM that if something that puts
>things in order is called "sort", then something that destroys order could
>simply be called "shuffle"


It think the name was intended to distinguish it from "perfect shuffle". A
perfect shuffle is when you shuffle two half decks together, and one card
from the left alternates with exactly one card from the right. (Eight
perfect shuffles results in the deck returning to its original order, by the
way.) Since a perfect shuffle leaves the deck in a completely predictable
order, the term "random shuffle" is used to refer to any method of
re-ordering the deck such that the results are random (or at least far less
predictable to the observer).

I shall now shuffle back to work...

-Howard

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please see statement encloded about postal and lottery laws Linda Rowe ASP .Net 3 06-17-2004 10:40 AM
lottery number thing problem moi C++ 6 11-12-2003 10:45 PM
Re: Ligit Lottery$ Ionizer Computer Support 1 08-01-2003 12:26 AM
mail lottery zubba Computer Support 0 07-17-2003 03:15 PM
419 gangs working in Spain with lottery scam Joel Rubin Computer Support 0 07-09-2003 02:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57