473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

UK lottery number generator

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

Apr 14 '07 #1
8 4343
On 14 Apr 2007 12:27:01 -0700, "Miktor" <bi************@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.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
Apr 14 '07 #2
On Apr 14, 8:44 pm, Jack Klein <jackkl...@spamcop.netwrote:
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! :)

Apr 14 '07 #3
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
Apr 14 '07 #4
On Apr 15, 3:27 am, "Miktor" <bigbadmick2...@hotmail.comwrote:
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?

Apr 14 '07 #5
On 14 Apr 2007 13:27:37 -0700, "livibetter" <li********@gmail.com>
wrote in comp.lang.c++:
On Apr 15, 3:27 am, "Miktor" <bigbadmick2...@hotmail.comwrote:
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.hadded 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
Apr 14 '07 #6
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
Apr 14 '07 #7
"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"
Apr 14 '07 #8

"osmium" <r1********@comcast.netwrote in message
news:58*************@mid.individual.net...
"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

Apr 16 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: moi | last post by:
hi, i have an assignment to put 6 pseudo random numbers into an array to simulate drawing 6 lottery numbers between 1-49. the code i have to do this so far is listed below. i dont think its far...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
0
by: NTL Newsgroups | last post by:
Hi, I've written a lottery programme in PHP that selects 10 winners a week from approximately 18,000 shares. The problem I have is that of the ten winners one week the same share number was...
11
by: Kevin Williamson | last post by:
Hi, I've written a lottery programme in PHP that selects 10 winners a week from approximately 18,000 shares. The problem I have is that of the ten winners one week the same share number was...
3
by: dazza2122 | last post by:
I want to design a program to genarate lottery number the number have to be unique and be 0 to 49. Can anybody shed some sort of light on the code it would take
0
by: pokerislotto | last post by:
We invent a method called pokerization of lottery to play lottery strategically. This is the website : http://lottoispoker.bravehost.com/ We deal with UK National Lottery, Canada Lotto 6/49 and...
8
by: ImortalSorrow | last post by:
Hey, I'm quite new in programing so need some help with this lottery program I'm making. What I'm trying to do is very simple, simulate a lottery but my effords are in vain since when I'm compiling...
4
by: 1q2w3e | last post by:
You are to create a lottery game program that allows a user to simulate playing the lottery. In this lottery game, the winning number combination comprises four unique numbers, each in the range of...
20
by: A | last post by:
Hi all. Is this a bug or what??? here is a simple code: <?php mt_srand(1); echo mt_rand(0, 255)."<br />"; echo mt_rand(0, 255)."<br />";
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.