Explain Codes LogoExplain Codes Logo

Generate random int value from 3 to 6

sql
random-number-generation
sql-server
database-performance
Anton ShumikhinbyAnton Shumikhin·Oct 6, 2024
TLDR

Execute the below SQL query to rapidly produce a random integer lying between 3 and 6:

SELECT CEILING(3 + RAND() * (6 - 3));

This script utilizes RAND() to generate a random fractional number, scales it to your desired range (3-6), and enforces CEILING() to ensure the result is rounded up as an integer within the specified bounds.

Quick alternatives and caveats

Different scenarios and requirements may call for unique approaches. Let's explore a few.

NEWID() and CHECKSUM – A powerhouse combo

In SQL Server, a combination of NEWID() and CHECKSUM() can generate randomness that hits right in the feels:

-- Be nice and random, like a bear choosing picnic baskets 🐻 SELECT ABS(CHECKSUM(NEWID()) % 4) + 3;
  • NEWID() generates a unique random identifier for each row.
  • CHECKSUM() then converts this identifier into numerical form.
  • The modulus operation, % 4, leashes the result within a range of 0 to 3.
  • By adding 3, the minimum value is tweaked to hit our target, just how we like it.

To the Infinity and beyond – Dynamic range for random numbers

If you fancy a more dynamic approach that can roll with punches, this script can be tweaked to serve your noble mission:

-- I'm making random as easy as changing underwear. 💪 DECLARE @min int = 3; DECLARE @max int = 6; SELECT ABS(CHECKSUM(NEWID()) % (@max - @min + 1)) + @min;

The One - Avoiding Matrix glitches with row uniqueness

When unique keys per row are your quest, or you're wrestling with multiple rows, dodging RAND() is the order of the day, given its proclivity to reproduce duplicate values. Fear not, for salvation is in sight:

-- Rand() is like dejavu; it glitches with repeats. Neo isn't here though! 🔫 SELECT ABS(CHECKSUM(NEWID()) % 4) + 3 FROM YourTable;

For The Greater Good – Randomness with Purpose

CRYPT_GEN_RANDOM – Random's Secret Agent

For the times when RAND() doesn't quite cut it and you need a more secret and less predictable agent, CRYPT_GEN_RANDOM is your go-to-guy:

-- CRYPT_GEN_RANDOM: Rand() on steroids, without the shrinkage 👍 SELECT CRYPT_GEN_RANDOM(1) % 4 + 3;

In the CRYPT_GEN_RANDOM universe, the mission is to generate 1 byte of random data, which is then cordoned off using the modulus operation, followed by adjustment to fit within the 3-to-6 precincts.

Unpredictability at its best - PREDICT...NOT()

Keep in mind, while RAND() is no slouch and is easy to call upon, it's not quite the superhero in scenarios requiring high entropy such as strong cryptographic applications. Save those stares for other more deserving contenders like CRYPT_GEN_RANDOM in SQL Server.

Data type efficiency – TINYINT for the win!

When taking on larger data queries, remember to consider your data type needs. Using TINYINT, for instance, could be like slipping into skinny jeans for your storage woes if the range of numbers sits comfortably within the TINYINT boundaries (0 to 255).