SQL Server RAND() Function
Last Updated :
06 Sep, 2024
The RAND()
function in SQL Server generates pseudo-random floating-point numbers within the range of 0 (inclusive) and 1 (exclusive). It is a function that can produce different random values each time it is called unless a specific seed is provided which results in a repeatable sequence of numbers.
In this article, We will learn about SQL Server RAND() Function in detail with the help of various examples and so on.
SQL Server RAND() Function
- The
RAND()
function in SQL Server generates a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive).
- Each time we call
RAND()
, it returns a different random value unless you provide a specific seed.
Syntax
RAND() function syntax is:
RAND(seed)
Parameter
RAND() function accepts a parameter as given below:
- seed: If the seed is specified, it returns a repeatable sequence of random numbers.
- If no N is specified, it returns a completely random number. It is optional and it works as a seed value.
Key Points:
- Range: The value returned by
RAND()
is always greater than or equal to 0 and less than 1. - Deterministic with Seed: If a seed value is provided, the sequence of numbers generated by
RAND()
will be repeatable and deterministic. - Non-Deterministic without Seed: If no seed is provided, the function will produce different random numbers on each execution.
Example of SQL Server RAND() Function
Let's look at some examples of the RAND() function in SQL Server.
Example 1:
In this example, we return a random decimal value.
Query:
SELECT RAND();
Output:
0.37892290119984562
Example 2:
In this example, we return a random decimal value with seed value =5.
Query:
SELECT RAND(5);
Output :
0.71366652509795636
Example 3:
In this example, we will use RAND() function with variables and getting a random number between in the range of [ 2, 8 ) using RAND function.
Query:
DECLARE @i INT;
DECLARE @j INT;
SET @i = 2;
SET @j = 8;
SELECT FLOOR(@i + RAND()*(@j-@i));
Output :
7.0
This query declares two integer variables, @i
and @j
, and sets their values to 2
and 8
, respectively. It then generates a random floating-point number between @i
and @j
using the RAND()
function, and the FLOOR()
function is applied to round down the result to the nearest integer. The final output is a random integer between 2
and 7
.
Conclusion
The RAND()
function in SQL Server offers a straightforward way to generate random numbers, with the ability to control randomness through seeding. By providing a seed, you can ensure that the sequence of random numbers is reproducible, which is useful for testing and debugging. Without a seed, the function produces non-deterministic results, providing a different random number with each call.
Similar Reads
SQL Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Introduction of DBMS (Database Management System) A Database Management System (DBMS) is a software solution designed to efficiently manage, organize, and retrieve data in a structured manner. It serves as a critical component in modern computing, enabling organizations to store, manipulate, and secure their data effectively. From small application
8 min read
SQL Query Interview Questions SQL or Structured Query Language, is the standard language for managing and manipulating relational databases such as MySQL, Oracle, and PostgreSQL. It serves as a powerful tool for efficiently handling data whether retrieving specific data points, performing complex analysis, or modifying database
15 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. They enable calculations across a specific set of rows, known as a "window," while retaining the individual rows in the dataset. Unlike traditional aggregate functions that summarize data for the entire group, win
7 min read