Jump to content

Recommended Posts

Posted (edited)

Bom dia,

Fiz um código para sorteio de 50 Pokemons, mas ele escolhe apenas o primeiro nome do array. Como posso deixar aleatório? 

Eis o código:

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


int main() {
    char pokemon[51][15] = {"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;
    srand(time(NULL));
    
  	i = (rand() >> 11) % 3;
	printf ("%s",pokemon[i]);    
    
    system ("Pause");
    return 0;
}
Edited by Camila Oliveira
Posted

Consegui utilizando a biblioteca time.h e a função srand(time(NULL))
 

#include <stdlib.h>
#include <stdio.h>
#include <time.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;
	
	srand(time(NULL));
	i = rand() %50;
  	
	printf ("%d\n",i);    
	
	system ("Pause");
	return 0;
}
Posted
3 hours ago, Camila Oliveira said:

Foi uma instrução que eu encontrei aqui mesmo no fórum e que, segundo o membro, tornaria a função mais aleatória por usar bits mais significativos.

https://www.portugal-a-programar.pt/forums/topic/56684-sorteio-de-string-em-array/

Porque é que nunca respondem a pergunta... 

IRC : sim, é algo que ainda existe >> #p@p

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.