SA-MP Forums Archive
[Ayuda] Repeticiуn en valores random. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: Non-English (https://sampforum.blast.hk/forumdisplay.php?fid=9)
+--- Forum: Languages (https://sampforum.blast.hk/forumdisplay.php?fid=33)
+---- Forum: Español/Spanish (https://sampforum.blast.hk/forumdisplay.php?fid=29)
+---- Thread: [Ayuda] Repeticiуn en valores random. (/showthread.php?tid=622621)



[Ayuda] Repeticiуn en valores random. - GabrielBas - 24.11.2016

Como dice el titulo, tengo un problema en unos valores random, resulta, que se repiten... y muchas veces. Es un caso de un test de preguntas que hay mбs o menos 15 y se randomizan, pero sale como 5 veces la misma pregunta y eso molesta. їComo puedo hacer para que salga solo una vez esa pregunta y luego salgan mas pero sin repetirse?

El cуdigo es el siguiente.

Код:
stock Test(playerid, section)
{
	
    if(section == 1)
	{
	    Dialog_Show(playerid, QUIZ1, DIALOG_STYLE_LIST, "Antes que nada, їsabes de rol?"," (A) Sн\n (B) No","Seleccionar","");
	}
        else
	{
	    new rand = random(sizeof(QuizQuestions)); <-- valor random de las preguntas.

	    CreateSpacer(playerid, 10); <-- espaciador de chat, no interfiere.

	    format(QAnswer[playerid], 2, "%d", strval(QuizQuestions[rand][0]));

	    Dialog_Show(playerid, QUIZ2, DIALOG_STYLE_LIST, QuizQuestions[rand][1], QuizQuestions[rand][2],"Seleccionar","");

	    SendSplitMessage(playerid, COLOR_SLATEGRAY, QuizQuestions[rand][1]); <- no interfiere
	    SendSplitMessage(playerid, COLOR_WHITE, QuizQuestions[rand][2]); <- no interfiere
	}
	return 1;
}



Re: [Ayuda] Repeticiуn en valores random. - Unrea1 - 24.11.2016

Код:
// ** Declarando la variable.

new bool:preguntas_test[MAX_PLAYERS][MAX_PREGUNTAS];

// ** OnPlayerConnect o en la funciуn donde reseteas las variables.

for(new x = 0; x < MAX_PREGUNTAS; x++) preguntas_test[playerid][x] = false;

// ** Funciуn

stock Test(playerid, section)
{
    if(section == 1) return Dialog_Show(playerid, QUIZ1, DIALOG_STYLE_LIST, "Antes que nada, їsabes de rol?"," (A) Sн\n (B) No", "Seleccionar", "");

	new rand = random(sizeof(QuizQuestions));
	if(preguntas_test[playerid][rand] == true) return Test(playerid, section);
	preguntas_test[playerid][rand] = true;

	CreateSpacer(playerid, 10);

	format(QAnswer[playerid], 2, "%d", strval(QuizQuestions[rand][0]));

	Dialog_Show(playerid, QUIZ2, DIALOG_STYLE_LIST, QuizQuestions[rand][1], QuizQuestions[rand][2], "Seleccionar", "");

	SendSplitMessage(playerid, COLOR_SLATEGRAY, QuizQuestions[rand][1]);
	SendSplitMessage(playerid, COLOR_WHITE, QuizQuestions[rand][2]);
	return 1;
}

// ** OnPlayerDisconnect

for(new x = 0; x < MAX_PREGUNTAS; x++) preguntas_test[playerid][x] = false;
Йsta es una manera, en MAX_PREGUNTAS debes colocar el valor total de las preguntas, ya sea colocando el #define encima de todo o cambiбndolo sуlo por el nъmero total de las preguntas, saludos.


Respuesta: Re: [Ayuda] Repeticiуn en valores random. - Adoniiz - 25.11.2016

Quote:
Originally Posted by LatinZ
Посмотреть сообщение
Код:
// ** Declarando la variable.

new bool:preguntas_test[MAX_PLAYERS][MAX_PREGUNTAS];

// ** OnPlayerConnect o en la funciуn donde reseteas las variables.

for(new x = 0; x < MAX_PREGUNTAS; x++) preguntas_test[playerid][x] = false;

// ** Funciуn

stock Test(playerid, section)
{
    if(section == 1) return Dialog_Show(playerid, QUIZ1, DIALOG_STYLE_LIST, "Antes que nada, їsabes de rol?"," (A) Sн\n (B) No", "Seleccionar", "");

	new rand = random(sizeof(QuizQuestions));
	if(preguntas_test[playerid][rand] == true) return Test(playerid, section);
	preguntas_test[playerid][rand] = true;

	CreateSpacer(playerid, 10);

	format(QAnswer[playerid], 2, "%d", strval(QuizQuestions[rand][0]));

	Dialog_Show(playerid, QUIZ2, DIALOG_STYLE_LIST, QuizQuestions[rand][1], QuizQuestions[rand][2], "Seleccionar", "");

	SendSplitMessage(playerid, COLOR_SLATEGRAY, QuizQuestions[rand][1]);
	SendSplitMessage(playerid, COLOR_WHITE, QuizQuestions[rand][2]);
	return 1;
}

// ** OnPlayerDisconnect

for(new x = 0; x < MAX_PREGUNTAS; x++) preguntas_test[playerid][x] = false;
Йsta es una manera, en MAX_PREGUNTAS debes colocar el valor total de las preguntas, ya sea colocando el #define encima de todo o cambiбndolo sуlo por el nъmero total de las preguntas, saludos.
Debes tener en cuenta que si sobrepasa el mбximo de MAX_PREGUNTAS no va a funcionar, y por ende, causa un crash. Siempre se va a repetir la condiciуn porque ya todos estan en 1.


Respuesta: [Ayuda] Repeticiуn en valores random. - Zume - 25.11.2016

Creo que la mejor forma es almacenar de la forma en que LatinZ detectaba si un valor ya se habнa utilizado, pero desde un bucle pasar a otro array temporal de forma ordenada los datos y obtener los datos de las preguntas basandoce en ese nuevo array y la cantidad de datos que posee (en el parбmetro random) asн basicamente va eliminando datos que ya se utilizaron.

edit: en cuanto llegue a casa pongo un ejemplo en caso de que no se entienda