一个创建彩票游戏的程序,该彩票游戏可以有效地创建 10 套而没有任何重复?

A program to create a lottery game that creates 10 sets without any duplicates, efficiently?

本文关键字:游戏 创建 彩票 有效地 任何重 一个 程序      更新时间:2023-10-16

乐透游戏需要 5 个数字,范围从 1 到 35(包括 1 到 35)。使用随机数为乐透游戏生成 10 组 5 个数字。每组不能重复任何数字。

随机生成中奖号码,并确定有多少投注正确了 3 个或更多数字。

这是我接到的任务的一部分。我尝试在 for 循环中使用 for 循环来检查生成的集合中的重复项。我们不允许使用向量。

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(){
int i, j, x, num[5], win[5],n1, n2, count=0, check;
int match=0, goodBet=0;
srand(time(NULL));

//generates the winning numbers for the lottery
cout << "Winning numbers: " << endl;
for (i=0; i<5; i++){
win[i] = rand() % 35 + 1;   
}
for(i=0;i<5;i++){
check = win[i];
win[i]=0;
for(int ld =0;ld<5;ld++){
if(check == win[ld]){   
win[ld]=rand()% 35+1;   
}
}
win[i]=check;
}
//prints the lottery numbers
for (i=0; i<5; i++){
cout << win[i] << " ";
}
cout << endl << endl;
//generates 10 sets of "bets" to compare to the winning numbers of the 
lottery to find a winner
cout << "Bets: " << endl;
for (x=0; x<10; x++){
for (i=0; i<5; i++){
n1 = rand() % 35 + 1;//generates random number
num[i]=n1;
}
/*
Checks num array for duplicates.Assigns the check variable with 
the value at num[i]
afterwards assigns num[i] with 0 to check for duplicates in the 
array
*/
for(i=0;i<5;i++){
check = num[i];
num[i]=0;
for(int ld =0;ld<5;ld++){
if(check == num[ld]){
count++;
num[ld]=rand()% 35+1;   
}
}
num[i]=check;
}
for (i=0; i<5; i++){
cout << num[i] << " ";
}
cout << endl;
for (i=0; i<5; i++){
for (j=0; j<5; j++){
if (num[i] == win[j]){
match++;
}
}
if (match >= 3){
goodBet++;
}
}
match=0;
}
cout << endl << goodBet << " bet/s got 3 or more numbers correct" << 
endl;
return 0;
}

开奖号码:

10 34 4 25 2

赌注:

20 18 21 25 14

8 9 14 11 6

13 4 24 8 2

35 6 29 28 32

3515 7 33 22

9 13 27 32 28

17 2 3 23 13

1328 19 33 6

29 12 28 34 9

35 3 2 13 17

0 个赌注正确 3 个或更多数字

我会选择只创建一个包含 1..35 的数组,然后在该数组中随机生成索引,在每次迭代时从数组中删除所选元素。 如果重复项不存在,则无需检查它们。

int nums[5];
int numsToChooseFrom[35];
int numsAvailable;
int chosenIndex;
srand(time(0));
for(int set = 0; set < 10; ++set){
for(int i = 0; i < 35; ++i){
numsToChooseFrom[i] = i + 1;
}
numsAvailable = 35;
for(int i = 0; i < 5; ++i){
chosenIndex = rand() % numsAvailable;
nums[i] = numsToChooseFrom[chosenIndex];
for (int j = chosenIndex+1; j < numsAvailable; ++j) {
numsToChooseFrom[j-1] = numsToChooseFrom[j];
}
--numsAvailable;
}
for(int i = 0; i < 5; ++i){
cout << nums[i] << " ";
}
cout << endl;
}

现场演示

似乎您正在尝试先生成五个随机数,然后检查重复项。随时检查重复项要简单得多。

for(int i=0;i<5;i++){
check = num[i];//assigns check the value in the array at position i
num[i]=0;//changes it to 0 so it doesn't count this as a duplicate
for(int ld=0;ld<5;ld++){
if(check == num[ld]{
num[ld]=rand()%35+1; // *** Wrong thing
}
}
num[i]=check;
}

使用更简单的算法:

  1. 选择一个随机数。不要将其存储在列表中。
  2. 将新的整数变量设置为零。
  3. 遍历已选择的数字列表,如果有任何与我们在步骤 1 中选择的数字匹配,则将步骤 2 中的整数设置为值 1。
  4. 如果整数的值为 1,请转到步骤 1。
  5. 将随机数添加到列表中。
  6. 如果列表有五个数字,请停止。否则,请转到步骤 1。

你可以让你的原始代码工作,你只需要改变你所做的,如果你找到一个匹配项。如果找到匹配项,则需要选择一个新的随机数并重新开始比较。因此,与其num[ld]=rand()%35+1;,不如check=rand()%35+1; ld=0;

你还想摆脱那里的num[i]=0;。整个数组需要已经设置为零。或者,或者,将ld<5更改为ld<i.

检查重复项的方式存在小错误。请考虑以下示例:

// Initially your list may look like this after having generated five random numbers
//   and you follow the above code to replace duplicates by new random numbers
22 14 22 32 11 
// Iteration 0
22 14 11 32 11 // 2nd index is changed
// Iteration 1
22 14 11 32 11 // No change as 14 is unique in the list
// Iteration 2
22 14 11 32 22 // 4th index is changed
// Iteration 3
22 14 11 32 22 // No change as 32 is unique in the list
// Iteration 4
14 14 11 32 22 // Index 0 is changed

请注意,列表中仍有重复项。因此,此问题的解决方案是在生成新的随机数时检查列表中存在的所有数字。

还有一件事,检查

if(check == num[ld]) num[ld]=rand()%35+1;不保证num[ld]不再与check相同。相反,可以使用while