# noinspection SqlUserVariableUsageForFile

SET @simulations := 100000;
SET @interval := 0.05;
SET @tours := 1000;
SET @startValue := 100.0;
SET @k := 0;
SET @median := NULL;

SELECT
	COUNT(finalValue) AS simulations,
	@tours AS nombreTours,
	ROUND(100*@interval) AS plusMoinXPc,
	ROUND(@startValue) AS valeurInitiale,
	AVG(finalValue) AS moyenne,
	@median AS mediane,
	MAX(finalValue) AS maxiValue,
	MIN(finalValue) AS miniValue,
	STD(finalValue) AS stdDeviation,
	VAR_POP(finalValue) AS populationStdVariance,
	VAR_SAMP(finalValue) AS sampleVariance
FROM (
		SELECT
			IF((@k := @k + 1) = FLOOR(@simulations/2), @median := s.finalValue, NULL) AS isMediane,
			s.*
		FROM (
			SELECT DISTINCT * FROM (
					SELECT
						i.n AS numeroTirage,
						@val := @startValue AS startValue,
						BENCHMARK(@tours, @val := @val * (1. - @interval + (RAND()*2.*@interval))),
						@val AS finalValue
					FROM integers AS i
					WHERE i.n BETWEEN 1 AND @simulations
			) AS t
			ORDER BY finalValue ASC
		) AS s
) AS s
;

