I would use the RAND()
function instead of NEWID()
.
Using RAND()
, we can generate a random number between 1 and 100, and then use a CASE
statement to select a type
based on the number randomly generated.
According to MSDN:
RAND()
returns a pseudo-random float value from 0 through 1,
exclusive
Meaning that multiplying RAND()
by 100 will give us a number from 0 to 99. Adding 1 changes the range to 1 to 100.
If after selecting which type
to return, you want to randomly select a record from that type
, you can then add a SELECT TOP 1
... ORDER BY NEWID()
to get a random record of that type:
DECLARE @Random INT
SET @Random = (RAND() * 100) + 1
SELECT TOP 1 ID, Type
FROM Items
WHERE Type = CASE
WHEN @Random > 30 THEN 3
WHEN @Random BETWEEN 6 AND 30 THEN 2
ELSE 1
END
ORDER BY NEWID()
See it here... run it a few times to see that the results match the probabilities.