Hi,
I would like to run a program that auto pick up 5 numbers from 45 (1 to 45), no duplicate number. The odd number must have higher drawn percentage. (mean the odd must be 3 numbers, even 2 numbers). And the result exclude all the past winning numbers. Examples for past winning numbers:
09-17-36-44-49
24-25-36-39-41
06-20-37-43-44
04-07-24-31-35
01-03-22-32-40
First, I hope this isn't going to be used for anything serious (like actual sweepstakes or gambling). If it is, then please stop; this is not something a beginner should be writing.
Second, what would you like us to do? You have a problem to solve, but haven't said what help you'd like from us.
(mean the odd must be 3 numbers, even 2 numbers)
Examples:
01-03-22-32-40
Your example does not fulfil the rule you said.
Think of real life lottery or cards. You have a set of unique items (balls or cards). You shuffle them. Then you pick some of them. Even if you deal from the top of the shuffled deck, you will get random values. You can't get the same item twice, unless you put them back after reading.
Hi, I don't understand how other conditions work in c++, how to get the odd/even weighting, how to shown result from min to max and exclude the past winning numbers (arrays), here is sample of pick up 5 numbers from 45:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <set>
#include <ctime>
#include <cstdlib>
#define Count 45
#define Balls 5
using namespace std;
int main()
{
int lotto[Count];
int x,draw,b;
srand((unsigned)time(NULL));
for(x=0;x<Count;x++)
lotto[x]=0;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <set>
#define Count 45
#define Balls 5
usingnamespace std;
int main() {
int lotto[Count];
int x, draw, b;
srand((unsigned)time(NULL));
for (x = 0; x < Count; x++) lotto[x] = 0;
draw = 0;
while (draw < Balls) {
b = rand() % Count;
if (lotto[b] == 0) {
lotto[b] = 1;
draw++;
}
}
puts("Lotto numbers:");
for (x = 0; x < Count; x++) {
if (lotto[x] == 1) {
printf("%3d", x + 1);
}
}
putchar('\n');
return (0);
}
Anyway... let's focus on the odd-even bias for now. What you could do is have two loops for drawing your balls: one sets three even-indexed variables on your array (the reasoning being that you add 1 when it comes time to print), and the other marks two odd-indexed variables on your array. That means doing something along the lines of what lastchance did in his example for generating the numbers (that is, approximately halving the right side of your % operator, multiplying the results 2, then either adding 0 or 1 depending on whether you want the number to be odd or even).
Side note, aside from the usingnamespace std bit and use of C++ headers, this is very much a C program. By C++ standards, it uses a lot of outdated methodology. Just in case ya didn't know.
#include <iostream>
#include <vector>
#include <numeric>
#include <chrono>
#include <random>
#include <algorithm>
void pick_even(int&, std::vector<unsigned>&);
void pick_odd(int&, std::vector<unsigned>&);
int main()
{
// maximum lottery number
constunsigned max_lotto = 45;
// create a vector with max_lotto elements, initalize elements to zero
std::vector<unsigned> lotto(max_lotto, 0);
// fill the vector from 1 to max_lotto
std::iota(lotto.begin(), lotto.end(), 1);
// let's see the lottery sequence unrandomized
unsigned count { 0 };
for (constauto& itr : lotto)
{
std::cout << itr << '\t';
++count;
if (0 == count % 10)
{
std::cout << '\n';
}
}
std::cout << "\n\n";
// obtain a time-based seed
unsigned seed = static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count());
// randomly shuffle the lottery numbers
std::shuffle(lotto.begin(), lotto.end(), std::default_random_engine(seed));
// let's see randomized lotto sequence
count = 0;
for (constauto& itr : lotto)
{
std::cout << itr << '\t';
++count;
if (0 == count % 10)
{
std::cout << '\n';
}
}
std::cout << "\n\n";
int num_to_pick {};
std::vector<unsigned> picked;
// select the first three odd numbers from the lottery vector
for (size_t loop = 0; loop < 3; loop++)
{
// pick an odd number from the lottery vector
pick_odd(num_to_pick, lotto);
std::cout << num_to_pick << '\t';
// add the picked number to the picked vector
picked.push_back(num_to_pick);
}
std::cout << '\n';
// select the first two even number from the lottery vector
for (size_t loop = 0; loop < 2; loop++)
{
// pick an even number from the lottery vector
pick_even(num_to_pick, lotto);
std::cout << num_to_pick << '\t';
// add the picked number to the picked vector
picked.push_back(num_to_pick);
}
std::cout << "\n\n";
std::cout << "Here are your picked numbers:\n";
for (constauto& itr : picked)
{
std::cout << itr << '\t';
}
std::cout << '\n';
}
// pick an even number from the lottery vector
void pick_even(int& num_to_pick, std::vector<unsigned>& vec)
{
// walkthrough the vector
for (auto itr = vec.begin(); itr != vec.end(); itr++)
{
// is the current element even?
if (0 == *itr % 2)
{
// stuff the current number into the reference
num_to_pick = *itr;
// erase the current element so it isn't picked again
vec.erase(itr);
return;
}
}
// no even number to pick from?
num_to_pick = -1; // error
return;
}
// pick an odd number from the lottery vector
void pick_odd(int& num_to_pick, std::vector<unsigned>& vec)
{
for (auto itr = vec.begin(); itr != vec.end(); itr++)
{
if (0 != *itr % 2)
{
num_to_pick = *itr;
vec.erase(itr);
return;
}
}
num_to_pick = -1; // error
return;
}
Trying to write this program using the C libraries only would be a whole lot more work, I doubt I could have done it in a reasonable time. Maybe some time next decade.