我应该如何为数组实现随机数或随机数算法以随机顺序显示引号?

Mr.*_*zar 0 c++ random shuffle

我已经知道有这种事情的答案,但我真的不知道如何在我的代码中实现它们.另外,除非必要,否则我想不再使用任何其他功能.这是我的代码:

int main()
{
 unsigned seed;
 seed = 1;
 srand(seed);
 std::string starFox[8];
 int x[8];
 starFox[0] = "Do a barrel roll!";
 starFox[1] = "Try a somersault!";
 starFox[2] = "Use bombs wisely!";
 starFox[3] = "Something is wrong with the G-diffuser";
 starFox[4] = "Can't let you do that, Star Fox";
 starFox[5] = "Hey Einstein, I'm on your side";
 starFox[6] = "Whoa! help me!";
 starFox[7] = "Daddy screamed REAL good before he died!";

 for(int i=0; i<8; i++)
 {
  int y = 0 + rand() % 8;
  x[i] = y;

  if (x[i-1]!=y || x[i-2]!=y || x[i-3]!=y || x[i-4]!=y || x[i-5]!=y || x[i-6]!=y || x[i-7]!=y)
  {//now I need to make a statement that makes sure each number appears once.
   std::cout << starFox[y] << "\n";}
 }
 std::cout << '\n';

 return 0;
}
Run Code Online (Sandbox Code Playgroud)

那么,我应该改变一下这个代码,使它在每次程序执行时生成随机数?

R S*_*hko 5

使用std :: random_shuffle

// Shuffle
std::random_shuffle(starFox, starFox + 8);

// And write to standard output
std::copy(starFox, starFox + 8,
          std::ostream_iterator<std::string>(std::cout, "\n"));
Run Code Online (Sandbox Code Playgroud)