Can you help me write program- auto pick up lotto numbers?

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

Thank you

Last edited on
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.

-Albatross
A std::discrete_distribution would be a good place to start for getting the odd/even weighting you are looking for.

http://www.cplusplus.com/reference/random/discrete_distribution/

An array to hold your five picked numbers to see if any particular picked number had already been selected.

Which pseudo random number engine you use is your choice.

http://www.cplusplus.com/reference/random/
Thanks. I just know the basic that is pick up 5 random numbers, now will try combine other conditions.
(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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <set>
#include <ctime>
#include <cstdlib>
using namespace std;


set<int> getLotto()
{
   set<int> S;
   while ( S.size() < 2 ) S.insert( 2 * ( rand() % 22 ) + 2 );
   while ( S.size() < 5 ) S.insert( 2 * ( rand() % 23 ) + 1 );
   return S;
}


int main()
{
   srand( time( 0 ) );
   for ( int i : getLotto() ) cout << i << ' ';
   // To do - exclude the (extremely low probability) previous sets
}



Alternatively, there are 409101 possible sets meeting your criteria - if you have already generated them then just choose one at random.
Last edited on
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;

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);
}
In the future, code blocks make it much more convenient for us to read your code, and I'd like to ask for you to use them.
[code]Your code here![/code]

Your code, formatted and put into said blocks:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <set>
#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;

  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 using namespace 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.

-Albatross
PLEASE learn to use code tags, they make reading and commenting on your source code MUCH easier.

Hint, you can edit your post and add them:

http://www.cplusplus.com/articles/jEywvCM9/

If I read the requirements correctly you need to randomly choose 3 odd and 2 even numbers from a list of 1 - 45.

No C libraries used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#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
   const unsigned 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 (const auto& 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 (const auto& 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 (const auto& 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;
}
1       2       3       4       5       6       7       8       9       10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
31      32      33      34      35      36      37      38      39      40
41      42      43      44      45

13      42      5       31      27      1       9       25      35      11
33      41      36      23      26      15      14      16      34      28
17      21      43      12      38      3       22      7       44      24
39      6       20      40      32      29      2       4       10      19
30      8       37      18      45

13      5       31
42      36

Here are your picked numbers:
13      5       31      42      36
Last edited on
And if you want the 5 picked numbers to be sorted into ascending order insert the following code before the for loop at line 89:

std::sort(picked.begin(), picked.end());

1       2       3       4       5       6       7       8       9       10
11      12      13      14      15      16      17      18      19      20
21      22      23      24      25      26      27      28      29      30
31      32      33      34      35      36      37      38      39      40
41      42      43      44      45

6       44      17      20      34      14      16      35      41      15
24      32      45      27      5       43      19      38      7       26
33      4       13      8       9       42      25      21      11      1
37      10      22      18      2       29      31      40      12      3
39      28      23      30      36

17      35      41
6       44

Here are your picked numbers:
6       17      35      41      44
Thank you very much.
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.
Topic archived. No new replies allowed.