Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Lottery Program (http://www.programmingforums.org/showthread.php?t=13516)

C.B. Jul 9th, 2007 1:08 AM

Lottery Program
 
How could I create a program to Pick lets say a Number from as many other numbers I enter, at random? And another program to generate random numbers?

Also, I know how to program a little in C++ and I have a compiler and all. This program doesn't sound to complex in my opinion. Any help to make would be great.

King Jul 9th, 2007 1:14 AM

Well start by learning how to generate random number. Google the c++ rand function. You will get more help if you make an effort first. At the very least Google random number in c++ and then try and throw something together. If that that point you can't get something working, post what you have so far and we would all be glad to help you out.

C.B. Jul 9th, 2007 1:15 AM

This is plenty help, I'll do that right now and see what I can come up with.


Edit. I noticed your from Ontario have there been any Sub 20 games there? For the Sub 20 World Cup.

King Jul 9th, 2007 1:21 AM

Quote:

Originally Posted by C.B. (Post 130347)
Edit. I noticed your from Ontario have there been any Sub 20 games there? For the Sub 20 World Cup.

No clue, didn't even know what the Sub 20 games were until I just googled it lol. Not a huge fan of soccer or sports in general. I like MMA (mixed martial arts).

C.B. Jul 9th, 2007 1:26 AM

Quote:

Originally Posted by King (Post 130348)
No clue, didn't even know what the Sub 20 games were until I just googled it lol. Not a huge fan of soccer or sports in general. I like MMA (mixed martial arts).

Ah, well if you have a chance you should catch a game just for the hell of it.

Anyways, I've read so far and I like what I'm reading. I can create a number within a disired range and if im correct could I make it to where it would only be say 3 digit numbers?
Then Use the rand function again to choose a number from one of those? Like maybe in a seperate program?

The reason Im doing this is because I want to start a little lottery with my WoW guild. Say perhaps everyone that wants a ticket(number) Could send a Mail(sorry if youve never played WoW and dont understand) with the money for the ticket. Then as I receieve the mails, Ill generate a number for them and write it down so no one gets it. Then when its time for the drawing enter said numbers and have the prog pick one set randomly.

MiKuS Jul 9th, 2007 1:49 AM

the way i see this program working is:
* user puts in 5 numbers (5 for arguements sake)
* validate the input to make sure there are only 5 and that they're numbers
* store the result in an array
* generate 5 random numbers and store them in an array
* compare the two arrays to see if they're the same
* if they're the same display congrats or bad luck

this could be any ammount of numbers, including one - except with one you wouldn't use an array (or would you?)

if this is wrong please correct me (i'm new to coding)

C.B. Jul 9th, 2007 1:50 AM

:

//7-8-07
#include <stdio.h>
#include <stdlib.h>

int main( void )
main()
{
//Define Variables
int seed;
cin.get();
double r;
long int M;
double x;
int y;
int z;
int count;
//Code
seed = 10000;
srand(seed);
M = 1000;
for(count=1; count<=20; ++count)
{
r = (  (double)rand() / ((double)(RAND_MAX)+(double)(1)) );
x = (r * M);
y = (int) x;
z = y + 1;
printf("random number %3d %5f %5f %5d %5d\n",count,r,x,y,z);
}
}


Ok this is what i got so far. Can you tell me some problems with it?
I put the int main( void ) so i could use the cin.get because the program closed as soon as i Tryed to use it. And now it wont decompile.

DaWei Jul 9th, 2007 7:00 AM

You're writing C++. Don't use stdio.h and stdlib.h. Don't use printf. Use <iostream>, cin, and cout. cin.get () will pause the program at the point of the statement. Therefore, put if right before the return.

If you have additional functionality issues, you need to explain them. If your program won't compile, include the compiler errors and warnings. Make sure they are all turned on.

Read the "How to Post a Question" thread.

Ben.Dougall Jul 9th, 2007 8:45 AM

What is with the random cin.get()?

also,
I would seed the generator with time, seeing as the seed is constant, the generator is not truly random.
:

srand( static_cast<int>( time(0) )

for casting, I would also use static_cast< type >( value ).

Also like Dawei said, use std::cout instead of printf, as this is a C++ program, not C.

Ben.Dougall Jul 9th, 2007 11:33 AM

I quickly made a solution for the first problem, it should compile from what i can tell, I'm at work, and don't have a C++ compiler handy. There is no error checking in this at all, and the quality of the code in general is lame, but I'm not gonna make a full out solution for a little thing like this.

:

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <time.h>
  5.  
  6. // Gets input from the user's keyboard, returns the input in the form of a double.
  7. double GetNumber( void )
  8. {
  9.         std::cout << "Enter Number or 0 to exit: ";
  10.         double ret;
  11.  
  12.         std::cin >> ret;
  13.         return ret;       
  14. }
  15.  
  16. // Retrieves the numbers from the user and stores/returns a vector of doubles.
  17. std::vector<double> GetNumbers( void )
  18. {
  19.         std::vector<double> nums;
  20.         for (;;)
  21.         {
  22.                 double num = GetNumber();
  23.                 if ( num = 0 )
  24.                         break;
  25.                 else
  26.                         nums.push_back( num );
  27.         }
  28.         return nums;       
  29. }
  30.  
  31. int main( void )
  32. {
  33.         std::vector<double> numbers = GetNumbers();
  34.  
  35.         std::cout << "What number from the list do you want to see? ( 1 - " << numbers.size() << ")";
  36.         int choice;
  37.  
  38.         std::cin >> choice;
  39.  
  40.         std::cout << numbers[choice - 1] << std::endl;
  41.  
  42.         return 0;
  43. }



All times are GMT -5. The time now is 2:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC