Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Lottery Program Error


  • Please log in to reply

#1
Granz00

Granz00

    Member

  • Member
  • PipPipPip
  • 226 posts
//	Filename:	Best Picks.cpp
//	Programmer:	Josh Gager
//	Purpose:	Suggests numbers to pick for the lottery


#include "stdafx.h"
#include <time.h>
#using <mscorlib.dll>
using namespace System;


struct Lotto
{
	int Pick_Five[54];		//	Stores randomly picked numbers for 1-55
	int Power_Ball[41];		//	Stores Randomly picked numbers for 1-42
};


int random(int);


int main()
{
	Lotto Best_Pick;		
	int T = 0;				//	Used for loops
	int Temp = 0;			//	Used to transfer numbers to different variables
	int Count_Best = 0;		//	Counts all of Pick_Five
	int Count_Ball = 0;		//	Counts all of Power_Ball
	
	//	Sets Pick_Five and Power_Ball to 0
	for(T=0; T<55; T++)
	{
		Best_Pick.Pick_Five[T] = 0;
	}
	for(T=0; T<42; T++)
	{
		Best_Pick.Power_Ball[T] = 0;
	}

	srand(time(NULL));

	for(T=0; T<5000000; T++)
	{
		Temp = random(54);
		Best_Pick.Pick_Five[Temp]++;
	}
	//	Once this is done running, Pick_Five has a total of 5mil numbers, as it should
	//	But Power_Ball increases from 0 to some random number, which it shouldn't do

	Console::WriteLine();

	//	Output the numbers and how many times they were picked
	for(T=1; T<56; T++)
	{
		Console::WriteLine("  {0}:\t{1}",T.ToString(), Best_Pick.Pick_Five[T-1].ToString());
		Count_Best += Best_Pick.Pick_Five[T-1];
	}
	for(T=0; T<42; T++)
	{
		Count_Ball += Best_Pick.Power_Ball[T];
	}

	Console::WriteLine("  {0}, {1}\n", Count_Best.ToString(), Count_Ball.ToString());
	//	Added Count_Best and Count_Ball to temporarily check how many numbers were being stored

	return 0;
}


//	Purpose:	Randomly generate a number from 0 to the Max
//	Recieved:	The highest number to be generated
//	Output:		A random number
random(int Limit)
{
	int Temp;
	Temp = rand() % (Limit + 1);
	return Temp;
}

In a nutshell, one of my variables, Power_Ball, is picking up numbers somehow. Any help?
  • 0

Advertisements


#2
The Hurricane

The Hurricane

    Member

  • Member
  • PipPip
  • 11 posts
Did you
//Random Number Generator
int seed = (((unsigned)time(NULL))%100);
srand(seed);
for (counter = 1; counter <= seed; counter++)
rand();
do something like that...


for(T=0; T<5000000; T++)
{
Temp = random(54);
Best_Pick.Pick_Five[Temp]++;
}
That may have something to do with it.
what is random(54)?
  • 0

#3
Granz00

Granz00

    Member

  • Topic Starter
  • Member
  • PipPipPip
  • 226 posts
The error was;

int Pick_Five[54]; // Stores randomly picked numbers for 1-55
int Power_Ball[41]; // Stores Randomly picked numbers for 1-42

Which should have been;

int Pick_Five[55];
int Power_Ball[42];

As for what random(54) is, look at the bottom of the code. I created a funtion called random().

Do you have a link to a tutorial on how this method works?

//Random Number Generator
int seed = (((unsigned)time(NULL))%100);
srand(seed);
for (counter = 1; counter <= seed; counter++)
rand();


I have never seen it done like that.
  • 0

#4
The Hurricane

The Hurricane

    Member

  • Member
  • PipPip
  • 11 posts
Thats how we do it in school... Im sure there are simpler ways but I like the long and hard way... Its to easy to get lost on shortcuts
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP