OpenGL Mathematics (GLM) library for Python providing comprehensive vector and matrix manipulation capabilities for graphics programming.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Random number generation and noise functions for procedural content generation, simulation, and creative applications. These functions provide various probability distributions and noise algorithms commonly used in graphics and game development.
Functions for generating random values with different probability distributions.
def setSeed(seed):
"""
Set the random seed for reproducible random sequences.
Args:
seed: Integer seed value
Example:
glm.setSeed(12345) # Set specific seed for reproducible results
"""
def linearRand(min_val, max_val):
"""
Generate random value(s) with uniform distribution.
Args:
min_val: Minimum value (scalar or vector)
max_val: Maximum value (same type as min_val)
Returns:
Random value(s) uniformly distributed between min_val and max_val
Example:
rand_float = glm.linearRand(0.0, 1.0) # Random float [0, 1]
rand_vec3 = glm.linearRand(glm.vec3(-1), glm.vec3(1)) # Random vec3 in cube
rand_int = glm.linearRand(-10, 10) # Random integer [-10, 10]
"""
def gaussRand(mean, deviation):
"""
Generate random value(s) with Gaussian (normal) distribution.
Args:
mean: Mean value of the distribution (scalar or vector)
deviation: Standard deviation (scalar or vector)
Returns:
Random value(s) with Gaussian distribution
Example:
normal_val = glm.gaussRand(0.0, 1.0) # Standard normal distribution
normal_vec = glm.gaussRand(glm.vec3(5), glm.vec3(2)) # Mean=5, σ=2
"""Functions for generating random points within geometric shapes and surfaces.
def circularRand(radius):
"""
Generate random point on a circle's circumference.
Args:
radius: Circle radius (scalar)
Returns:
vec2 representing random point on circle circumference
Example:
point_on_circle = glm.circularRand(5.0) # Random point on radius=5 circle
"""
def sphericalRand(radius):
"""
Generate random point on a sphere's surface.
Args:
radius: Sphere radius (scalar)
Returns:
vec3 representing random point on sphere surface
Example:
point_on_sphere = glm.sphericalRand(3.0) # Random point on radius=3 sphere
"""
def diskRand(radius):
"""
Generate random point inside a disk (2D circle).
Args:
radius: Disk radius (scalar)
Returns:
vec2 representing random point inside disk
Example:
point_in_disk = glm.diskRand(2.0) # Random point inside radius=2 disk
"""
def ballRand(radius):
"""
Generate random point inside a ball (3D sphere).
Args:
radius: Ball radius (scalar)
Returns:
vec3 representing random point inside ball
Example:
point_in_ball = glm.ballRand(4.0) # Random point inside radius=4 ball
"""Procedural noise functions for generating smooth, pseudo-random patterns commonly used in procedural generation and texture synthesis.
def perlin(p):
"""
Generate Perlin noise value.
Args:
p: Input coordinate (scalar, vec2, vec3, or vec4)
Returns:
Noise value typically in range [-1, 1]
Note:
Perlin noise provides smooth, natural-looking random variation
with consistent gradients across space
Example:
noise_1d = glm.perlin(5.3) # 1D noise
noise_2d = glm.perlin(glm.vec2(x, y)) # 2D noise for textures
noise_3d = glm.perlin(glm.vec3(x, y, z)) # 3D noise for volumetric effects
"""
def simplex(p):
"""
Generate simplex noise value.
Args:
p: Input coordinate (scalar, vec2, vec3, or vec4)
Returns:
Noise value typically in range [-1, 1]
Note:
Simplex noise is an improved version of Perlin noise with
better visual characteristics and performance in higher dimensions
Example:
simplex_2d = glm.simplex(glm.vec2(x * 0.01, y * 0.01)) # Terrain height
simplex_3d = glm.simplex(glm.vec3(x, y, time)) # Animated noise
"""from pyglm import glm
import random
# === Basic Random Generation ===
# Set seed for reproducible results
glm.setSeed(42)
# Generate random floats
random_float = glm.linearRand(0.0, 1.0) # [0, 1]
random_range = glm.linearRand(-5.0, 5.0) # [-5, 5]
# Generate random vectors
random_position = glm.linearRand(glm.vec3(-10), glm.vec3(10)) # Random position in cube
random_color = glm.linearRand(glm.vec3(0), glm.vec3(1)) # Random RGB color
# Generate random integers
random_int = glm.linearRand(-100, 100) # Random integer
# Gaussian (normal) distribution
normal_value = glm.gaussRand(50.0, 10.0) # Mean=50, std_dev=10
height_variation = glm.gaussRand(0.0, 0.5) # Small height variation
# === Geometric Random Sampling ===
# Random points on geometric shapes
circle_point = glm.circularRand(1.0) # Unit circle circumference
sphere_point = glm.sphericalRand(5.0) # Sphere surface (radius=5)
# Random points inside shapes
disk_point = glm.diskRand(3.0) # Inside disk (radius=3)
ball_point = glm.ballRand(2.0) # Inside ball (radius=2)
# === Procedural Generation Examples ===
# Random terrain height generation
def generate_terrain_height(x, z, octaves=4):
height = 0.0
amplitude = 1.0
frequency = 0.01
for i in range(octaves):
noise_value = glm.perlin(glm.vec2(x * frequency, z * frequency))
height += noise_value * amplitude
amplitude *= 0.5 # Reduce amplitude for each octave
frequency *= 2.0 # Increase frequency for each octave
return height
# Generate terrain for a 100x100 grid
terrain_heights = []
for x in range(100):
row = []
for z in range(100):
height = generate_terrain_height(x, z)
row.append(height)
terrain_heights.append(row)
# Random particle system
class ParticleSystem:
def __init__(self, count):
self.particles = []
for _ in range(count):
# Random position in sphere
position = glm.ballRand(10.0)
# Random velocity on sphere surface (outward)
velocity = glm.sphericalRand(glm.linearRand(1.0, 5.0))
# Random color
color = glm.linearRand(glm.vec3(0.5), glm.vec3(1.0))
# Random lifetime
lifetime = glm.gaussRand(3.0, 0.5) # 3±0.5 seconds
self.particles.append({
'position': position,
'velocity': velocity,
'color': color,
'lifetime': max(0.1, lifetime) # Ensure positive lifetime
})
# Create particle system
particles = ParticleSystem(1000)
# === Noise-Based Procedural Generation ===
# 2D texture generation using Perlin noise
def generate_noise_texture(width, height, scale=0.1):
texture = []
for y in range(height):
row = []
for x in range(width):
# Generate noise value
noise_coord = glm.vec2(x * scale, y * scale)
noise_value = glm.perlin(noise_coord)
# Convert from [-1, 1] to [0, 1]
normalized = (noise_value + 1.0) * 0.5
# Convert to grayscale color
color = glm.vec3(normalized)
row.append(color)
texture.append(row)
return texture
# Generate 256x256 noise texture
noise_texture = generate_noise_texture(256, 256, 0.02)
# Animated noise for effects
def animated_noise(position, time):
# Use time as third dimension for animation
coord = glm.vec3(position.x * 0.05, position.y * 0.05, time * 0.1)
return glm.simplex(coord)
# Generate animated fire effect
def fire_effect(x, y, time):
# Multiple noise octaves for complex fire pattern
base_coord = glm.vec2(x * 0.01, y * 0.01 - time * 0.5) # Rising motion
# Large-scale noise for overall shape
large_noise = glm.perlin(base_coord) * 0.5
# Medium-scale noise for detail
medium_coord = base_coord * 3.0
medium_noise = glm.perlin(medium_coord) * 0.3
# Small-scale noise for fine detail
small_coord = base_coord * 8.0
small_noise = glm.perlin(small_coord) * 0.2
# Combine noises
combined_noise = large_noise + medium_noise + small_noise
# Add height-based falloff (fire dies out upward)
height_factor = max(0.0, 1.0 - (y * 0.01))
return combined_noise * height_factor
# === Random Distribution Utilities ===
# Generate random colors with specific hue
def random_color_with_hue(base_hue, saturation=1.0, value=1.0):
# Vary hue slightly
hue_variation = glm.gaussRand(0.0, 0.1)
final_hue = base_hue + hue_variation
# Add slight variations to saturation and value
final_sat = glm.clamp(saturation + glm.gaussRand(0.0, 0.1), 0.0, 1.0)
final_val = glm.clamp(value + glm.gaussRand(0.0, 0.1), 0.0, 1.0)
# Convert HSV to RGB (simplified - actual implementation would need HSV conversion)
return glm.vec3(final_sat * final_val) # Simplified for example
# Generate random movement patterns
class RandomWalker:
def __init__(self, start_position):
self.position = start_position
self.velocity = glm.vec3(0)
def update(self, dt):
# Add random acceleration
random_force = glm.sphericalRand(1.0) * glm.linearRand(0.5, 2.0)
self.velocity += random_force * dt
# Apply drag
self.velocity *= 0.95
# Update position
self.position += self.velocity * dt
def get_position(self):
return self.position
# Create random walker
walker = RandomWalker(glm.vec3(0, 0, 0))
# Simulate movement
for frame in range(100):
walker.update(1.0/60.0) # 60 FPS
position = walker.get_position()
# Use position for rendering...
# === Noise-Based Terrain Generation ===
def generate_island_terrain(x, z, island_size=50.0):
# Distance from center for island falloff
center_distance = glm.length(glm.vec2(x, z)) / island_size
island_falloff = 1.0 - glm.smoothstep(0.0, 1.0, center_distance)
# Multiple octaves of Perlin noise
detail_scale = 0.02
large_features = glm.perlin(glm.vec2(x * detail_scale, z * detail_scale)) * 10.0
medium_scale = 0.05
medium_features = glm.perlin(glm.vec2(x * medium_scale, z * medium_scale)) * 5.0
fine_scale = 0.1
fine_features = glm.perlin(glm.vec2(x * fine_scale, z * fine_scale)) * 2.0
# Combine noise layers
total_height = large_features + medium_features + fine_features
# Apply island falloff
final_height = total_height * island_falloff
# Ensure minimum ocean level
return max(final_height, -2.0)
# Generate island heightmap
island_size = 100
heightmap = []
for z in range(-island_size, island_size + 1):
row = []
for x in range(-island_size, island_size + 1):
height = generate_island_terrain(x, z)
row.append(height)
heightmap.append(row)setSeed() at the start of your application for reproducible results during developmentInstall with Tessl CLI
npx tessl i tessl/pypi-pyglm