codeAPI Reference

Sandbox.Random

calendar_today May 3, 2026 schedule ~1 min read person patrickjr verified 50

Sandbox.Random

Random number generation.

Overview

Random provides methods for generating random numbers, vectors, and making random selections.

Basic Random

CSHARP
// Random float 0-1
float f = Random.Shared.Float();

// Random float range
float f = Random.Shared.Float(10, 50); // 10 to 50

// Random int
int i = Random.Shared.Int(0, 100); // 0 to 99

Vectors

CSHARP
// Random vector
Vector3 v = Random.Shared.Vector3(-100, 100); // Each axis -100 to 100

// Random in circle
Vector2 circle = Random.Shared.VectorInCircle() * radius;

// Random in sphere
Vector3 sphere = Random.Shared.VectorInSphere() * radius;

// On unit sphere surface
Vector3 surface = Random.Shared.VectorOnSphere();

Booleans

CSHARP
// 50/50 chance
bool coin = Random.Shared.Bool();

// Weighted
bool chance = Random.Shared.Bool(0.3f); // 30% true

From Arrays

CSHARP
// Random element
string[] names = { "Alice", "Bob", "Carol" };
string pick = names[Random.Shared.Int(names.Length)];

// Or using System.Random
var random = new Random();
int index = random.Next(names.Length);

Seeds

CSHARP
// Create with seed (reproducible)
var rng = new Random(12345);
float num = rng.Float();

// Same seed = same sequence

Use Cases

CSHARP
// Random spread
Vector3 spread = Random.Shared.Vector3(-5, 5);
var bulletDir = aimDir + spread;

// Random color
Color c = Color.Random;

// Jitter
float pitch = Random.Shared.Float(-0.1f, 0.1f);
Was this helpful?