【问题标题】:Generate random number using given number. C++使用给定的数字生成随机数。 C++
【发布时间】:2015-10-29 15:55:59
【问题描述】:

我遇到了一个问题。我正在为我的家庭作业制作 Rock-Paper-Scissors 游戏,但我不知道如何仅使用 3 个特定给定数字生成数字并使用 char 和 ASCII 转换为字符 那些给定的数字是: 66、71和75

【问题讨论】:

  • “使用给定数字生成随机数”是什么意思?
  • 我想你想要的是 'B', 'G', 'L' 而不是幻数 66, 71, 75。

标签: c++ random


【解决方案1】:
#include <stdio.h>
#include <stdlib.h>
#include <time.h> 

char getRandomChar()

{
    srand(time(NULL));
    auto randomnumber = rand() % 3;
    char buf[3]={66, 71 , 75};
    return buf[randomnumber];
}

【讨论】:

  • 我认为你需要 ,除非它包含在
【解决方案2】:

这是我的解决方案。

#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

int main ()
{
  /* initialize random seed: */
  srand (time(NULL));

  enum Choice {ROCK,PAPER,SCISSORS};

  do {
      printf("Please enter your choice: ");
      char yours; //your choice
      scanf(" %c",&yours);

      if (yours=='q'){
          break;
      }

      Choice computers = static_cast<Choice>( rand() % 3 );

      if ((yours=='r' && computers==ROCK)||
          (yours=='p' && computers==PAPER)||
          (yours=='s' && computers==SCISSORS))
      {
          printf("Its a draw.\n\n");
          continue;
      }

      if ((yours=='r' && computers==SCISSORS)||
          (yours=='p' && computers==ROCK)||
          (yours=='s' && computers==PAPER))
      {
          printf("Congratulations, you won!\n\n");
      } else {
          printf("Sorry, you loose :-( \n\n");
      }

  } while(true);

  return 0;
}

使用字符 rps 在 ASCII 表中接近这一事实存在一个 hacky 解决方案。 p 在 ASCII 中的位置是 112。您可以计算 rand()%4+112 并将其与转换为 int 的 char 进行比较。

【讨论】:

    【解决方案3】:

    此代码将选择您的三个数字之一,在这种情况下,它将它们输出到输出流...

    #include <iostream> 
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
        srand(time(0)); // ensure truly randomized number   
    
        int nums[3] = {66, 71, 75};
        int i = rand() % 3; // find random number between 0 and 2
    
        cout << nums[i] << endl;
    
        return 0;
    }
    

    【讨论】:

    • “确保真正随机的数字” 抱歉,我没有选择。
    【解决方案4】:

    这是使用 ASCII 数字的解决方案。它将为您节省几行代码。

    #include <stdio.h>      /* printf, scanf, puts, NULL */
    #include <stdlib.h>     /* srand, rand */
    #include <time.h>       /* time */
    
    int main ()
    {
      /* initialize random seed: */
      srand (time(NULL));
    
      enum Choice {PAPER=112, QUIT, ROCK, SCISSORS};
    
      do {
          printf("Please enter your choice (r/p/s; q to quit): ");
          char yours; //your choice
          scanf(" %c",&yours);
    
          if (yours=='q'){
              break;
          }
    
          Choice computers;
          do {
            computers = static_cast<Choice>( 112 + rand() % 4  );
          } while (computers==QUIT);
    
          if (yours==computers)
          {
              printf("Its a draw.\n\n");
              continue;
          }
    
          if ((yours=='r' && computers==SCISSORS)||
              (yours=='p' && computers==ROCK)||
              (yours=='s' && computers==PAPER))
          {
              printf("Congratulations, you won!\n\n");
          } else {
              printf("Sorry, you loose :-( \n\n");
          }
    
      } while(true);
    
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 2014-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多