Class Random
- Namespace
- YndigoBlue.Velocity.Functions
- Assembly
- YndigoBlue.Velocity.dll
Represents the RANDOM mathematical function that generates a random number.
public class Random : Function, ICheckItem, IDefaultItem, IFilterItem, IElement
- Inheritance
-
Random
- Implements
Remarks
When called with no arguments, RANDOM returns a pseudo-random floating-point value in [0, 1). When called with two arguments (lower, upper), it returns a random value within that range.
Database-specific SQL varies: SQL Server and MySQL use RAND(), PostgreSQL uses
RANDOM(), Oracle uses DBMS_RANDOM.VALUE, SQLite uses RANDOM(), and
Teradata uses RANDOM(lower, upper). Velocity selects the appropriate form automatically.
RANDOM is not cryptographically secure and should not be used for security-sensitive operations.
Examples
Select ten random articles from a content table:
var schema = manager.LoadSchema("content");
var articles = schema["articles"];
var query = new Query()
.Select([ articles["id"], articles["title"] ])
.From(articles)
.OrderBy(new Expression("rnd", new Random()))
.Limit(10);
var results = manager.Retrieve(query);
Generate a random integer within a hard-coded inclusive range using the integer literal constructor:
// Generates a random integer between 1 and 100 (inclusive)
var expr = new Expression("roll", new Random(1, 100));
var query = new Query().Select([ expr ]);
var results = manager.Retrieve(query);
Use the column + column constructor to generate a random value within per-row bounds stored in columns:
var schema = manager.LoadSchema("games");
var loot = schema["loot_table"];
Column minColumn = loot["min_roll"];
Column maxColumn = loot["max_roll"];
var query = new Query()
.Select([ loot["item_id"], new Expression("roll", new Random(minColumn, maxColumn)) ])
.From(loot);
var results = manager.Retrieve(query);
Mix a literal lower bound with a column upper bound, or a column lower bound with a literal upper bound:
var schema = manager.LoadSchema("games");
var loot = schema["loot_table"];
Column maxColumn = loot["max_roll"];
Column minColumn = loot["min_roll"];
// Fixed floor of 1.0, variable ceiling from column
var exprA = new Expression("roll_a", new Random(1.0, maxColumn));
// Variable floor from column, fixed ceiling of 100.0
var exprB = new Expression("roll_b", new Random(minColumn, 100.0));
Constructors
- Random()
Initializes a new instance of RANDOM with no arguments, generating a value between 0 and 1.
- Random(IEnumerable<IElement>)
Initializes a new instance of RANDOM for multiple elements.
- Random(double, Column)
Initializes a new instance of RANDOM with a double lower bound and an upper bound column.
- Random(int, int)
Initializes a new instance of RANDOM with integer lower and upper bound literals.
- Random(IElement)
Initializes a new instance of RANDOM for a single element.
- Random(Column, double)
Initializes a new instance of RANDOM with a lower bound column and a double upper bound.
- Random(Column, Column)
Initializes a new instance of RANDOM with both lower and upper bound columns.