1

我想在 1 到 50 个口袋妖怪之间随机选择,但rand只选择阵列中的第 42 个(Dragonite)。我怎样才能让它变得随机?

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

int main(){

    char sorteio1[50][11] = {"Bulbasaur","Venusaur","Charmander","Charmeleon","Charizard","Pidgey","Pidgeotto","Pidgeot","Pikachu","Raichu","Clefairy","Vulpix","Ninetales","Meowth","Psyduck","Golduck","Mankey","Primeape","Growlithe","Arcanine","Abra","Kadabra","Alakazam","Magnemite","Magneton","Onix","Cubone","Marowak","Staryu","Starmie","MrMime","Jynx","Magikarp","Gyarados","Lapras","Ditto","Eevee","Vaporeon","Porygon","Snorlax","Dragonair","Dragonite","Mewtwo","Mew","Chikorita","Sentret","Furret","Hoothoot","Lanturn","Pichu"};
    int i;

    i = rand() %50;

    printf ("%s\n",sorteio1[i]);    

    system ("Pause");
    return 0;
}
4

1 回答 1

0

您应该播种随机数 - 您可以使用当前时间为您提供不同的输入。

检查srand 文档

文档中的示例:

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

int main ()
{
  printf ("First number: %d\n", rand()%100);
  srand (time(NULL));
  printf ("Random number: %d\n", rand()%100);
  srand (1);
  printf ("Again the first number: %d\n", rand()%100);

  return 0;
}
于 2018-09-21T16:25:16.167 回答