c - 防止重复随机创建的相同数字

标签 c random

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void) {

    int i,j,z,w,c; /* counter*/
    int ticket[6],n[6];
    int a; /*numbers of one ticket*/
    int x; /* Random numbers of tickets*/
    int num; /*Quantity of tickets*/
    int loser = 0; /*initialize the number of known-tickets*/
    int threeknown = 0;
    int fourknown = 0;
    int fiveknown = 0;
    int winner = 0;


    srand (time(NULL));


    printf ("Please enter the lucky ticket numbers between 0 and 50\n");

    for (i=0;i<6;i++) { /* loop for entering numbers of ticket from keyboard*/
        scanf ( "%d",&a);
        if (a<50 && a>0){
        ticket[i] = a;
        }
        else {
             printf ("\a ERROR: Please enter number between 0 and 50\n");
             i--; /* enter again */
        }
        }
    printf ("Lucky ticket is:\n");
    for (i=0;i<6;i++) {
        printf ("%3d",ticket[i]);
        }
        printf ("\n");
    printf ("Please enter the quantity of tickets\n\a");
    scanf ("%d",&num);
    for (z=0;z<num;z++) {  /* For each ticket */
        for (j=1;j<=6;j++) {
            x = 1 + rand()%49;
            n[j] = x;
            printf ("%3d",n[j]);
            }

            printf("\n\n");
            }
    for (z=0;z<num;z++){  /*counter for each ticket control */
        if (ticket[0]==n[0] && ticket[1]==n[1] && ticket[2]==n[2] && ticket[3]==n[3] && ticket[4]==n[4] && ticket[5]==n[5]) {
                            winner += 1;
                            }
        if (ticket[0]==n[0] && ticket[1]==n[1] && ticket[2]==n[2] && ticket[3]==n[3] && ticket[4]==n[4]) {
                            fiveknown += 1;
                            }
        if (ticket[0]==n[0] && ticket[1]==n[1] && ticket[2]==n[2] && ticket[3]==n[3]) {
                            fourknown += 1;
                            }
        if (ticket[0]==n[0] && ticket[1]==n[1] && ticket[2]==n[2]) {
                            threeknown += 1;
                            }
        else {
             loser += 1;
             }
                             }
        printf ("Number of winners : %d\n",winner);
         printf ("Number of five-knowns : %d\n",fiveknown);
          printf ("Number of four-knowns : %d\n",fourknown);
           printf ("Number of three-knowns : %d\n",threeknown);
              printf ("Number of losers : %d\n",loser);


        system ("PAUSE");
        return 0;

        }

我有一个关于 C 编码宾果程序的项目。我需要通过键盘在 0 到 50 (12 34 23 11 47 4) 之间维护一张中奖票,然后我需要随机生成票。在这些门票中,没有一张具有相同的号码,并且排序并不重要。例如(23 12 10 4 9 46)。我的问题是如何获得这些门票?我不想有这种票 ( 12 43 20 12 9 4 )

最佳答案

构建一个包含五十个可接受值的数组,选择一个,然后将其从数组中删除。

由于数组中的顺序并不重要,您可以通过用最后一个值覆盖它并减少“数组计数”变量来以很小的成本删除它。

void RemoveFromArray(int* arr, size_t *pNumberOfElements, size_t indexToRemove)
{
    //Note: I'm not putting the error/bounds checking because it's not what the question is about.
    size_t indexLast = *pNumberOfElements - 1;
    arr[indexToRemove] = arr[indexLast];
    (*pNumberOfElements)--;
}

void ChooseRandom6of50(int* random6ints)
{
    int arr[50];
    size_t nElements = 50;
    {
        size_t i;
        for(i=0 ; i<50 ; i++)
            arr[i] = (int)(i+1); //Fill with values 1 to 50
    }
    {
        size_t iDest;
        for(iDest=0 ; iDest<6 ; iDest++)
        {
            int rnd = rand() % nElements; //The real code should use the more elaborate random formula
            size_t randIndex = rnd;
            random6ints[iDest] = arr[randIndex];
            RemoveFromArray(arr, &nElements, randIndex);
        }
    }


}

关于c - 防止重复随机创建的相同数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20912356/

相关文章:

c - dlib 库在 Visual Studio 2010 中的使用

c - 当套接字设置为非阻塞时,有什么方法可以在阻塞模式下临时使用 send 或 recv

c - 从指向数组的指针使用 memcpy

javascript - 使用 Javascript 在 HTML5 Canvas 中随机化 RGB,为每个 fillRect 设置一个新值

c# - 在 C# 的 ListView 中删除相同的数字

javascript - 为什么使用 Math.random() 以及它的作用

c - 如何在 C 语言的 Windows 上安装访问冲突错误的信号处理程序?

C:全局定义多维char数组

c++ - 泊松分布总是在每次运行时产生相同的实例

mysql - 如何使用mysql随机将行转换为列