Reference > Functions > Mathematical
Rand
- 2005
- 2008
- 2012
- 2014
- 2016
- 2017
- 2019
- 2022
- 2005, 2008, 2012, 2014, 2016, 2017, 2019, 2022
Rand (Random) Mathematical Function
Use the Rand
function to return random value, optionally using a seed value.
Microsoft SQL Server does not require a FROM clause to execute the Rand
function, but dbExpression requires a FROM clause to execute any query. For this function to execute on it's own, a "dummy" FROM clause must be provided.
Syntax
db.fx.Rand({seed_expression})
Arguments
- seed_expression
Returns
float
Examples
Select Statement
Select a random value from the database.
float? result = db.SelectOne(
db.fx.Rand()
)
.From(dbo.Product)
.Execute();
Select Statement with Seed
Select a random value using a product's id as a seed value.
IEnumerable<float> results = db.SelectMany(
db.fx.Rand(dbo.Product.Id)
)
.From(dbo.Product)
.Execute();