samedi 22 mai 2021

How do you generate multiple unique random numbers within a range in mysql

I am working on a project where I print out 3 values each being within a certain range as shown in the example below. I pass in a value x.

I have tried this formula for if x = 1. This being:

SELECT (FLOOR(2 + RAND() * 32)) AS 'Low Range',
(FLOOR(33 + RAND() * 65)) AS 'Mid Range',
(FLOOR(66 + RAND() * 98)) AS 'High Range';

Unfortunately, this does not give me the desired results.

I also tried this formula:

SELECT FLOOR(RAND() * (33) + (x * 100)) AS 'Low Value',
(FLOOR(RAND() * (66) + (x * 100))) AS 'Mid Value',
(FLOOR(RAND() * (99) + (x * 100))) AS 'High Value';

But this does not work.

When I use:

SELECT FLOOR(RAND() * (200 - 100) + (x * 100)) AS RandomValue;

I can get the desired results for a single range between 100 and 200, or 200 and 300, but I cant figure out how to get 3 ranges.

This is what I would hope to achieve based on the value of x:

if x = 1

2 - 32, 33-65, 66-98

if x = 2

102 - 132, 133-165, 166-198

if x = 3

202 - 232, 233-265, 266-298

etc..

Essentially, I would like to increase by 100 for each increase of value of x. Is there a way I could achieve these results?




Aucun commentaire:

Enregistrer un commentaire