Module:Aléa

Une page de Wikiversité, la communauté pédagogique libre.

La fonction p.choix contenue dans ce module renvoie l'un de ses arguments au hasard.

La fonction p.math_random reproduit le fonctionnement de la fonction math.random(m[,n]), avec les arguments m et n facultatifs ; elle retourne un entier aléatoire entre m et n où un réel entre 0 et 1.

Voir manuel en français : https://www.mediawiki.org/w/index.php?title=Extension:Scribunto/Lua_reference_manual/fr&oldid=2241010#math.random

-> Accès à la documentation


local p = {}

function p.math_random(frame)   -- Scribunto math.random(m, n)
	local m = frame.args[1]     -- Collecte premier argument 
	local n = frame.args[2]     -- Collecte second argument
	math.randomseed( os.time() ) -- Graine aléatoire
	local r = math.random(m, n) -- m et n facultatif 
	return r                    -- retourne un nombre aléatoire entre m et n
end


function p.choix(frame)
	local nombre, pointe, temps = 1,1,1
	while frame.args[nombre] do
		nombre = nombre + 1
	end
	nombre = nombre - 1
	temps = os.clock()
	pointe = (nombre^2 + 3)^2
	temps = os.clock() - temps
	temps = temps*100000000
	math.randomseed(temps)
	pointe = math.random(nombre)
	return frame.args[pointe]
end

function p.pioche(frame)
	local nombre, temps = 1,1
	temps = os.clock()
	nombre = (7^2 + 3)^2
	temps = os.clock() - temps
	temps = temps*100000000
	nombre = tonumber(frame.args[1])
	math.randomseed(temps)
	return math.random(nombre)
end

function p.choix_rythme()
	local rythmes = mw.loadData('Module:Lilypond')
	local nombre = 0
	while rythmes[nombre + 1] do -- Compte le nombre de rythmes dans le tableau
		nombre = nombre + 1
	end
	local temps,index = 1,1
	temps = os.clock()
	index = (7^2 + 3)^2
	temps = os.clock() - temps
	temps = temps*100000000
	math.randomseed(temps)
	index = math.random(nombre) -- Détermine un index au hasard
	return rythmes[index] -- Retourne le rythme correspondant
end

return p