UK lottery number generator

M

Miktor

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
 
J

Jack Klein

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
 
M

Miktor

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! :)
 
O

Obnoxious User

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]
 
L

livibetter

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?
 
J

Jack Klein

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
 
O

Obnoxious User

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
 
O

osmium

Miktor said:
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"
 
H

Howard

osmium said:
:

. 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top