Skip to content

Commit

Permalink
Adding explanation to seeded rng used in examples (#12593)
Browse files Browse the repository at this point in the history
# Objective

- Fixes #12544

## Solution

- Added/updated a universally worded comment to all seeded rng instances
in our examples.
  • Loading branch information
andristarr committed Mar 26, 2024
1 parent b7ab146 commit d39ab55
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/3d/spotlight.rs
Expand Up @@ -49,6 +49,9 @@ fn setup(
));

// cubes

// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
let cube_mesh = meshes.add(Cuboid::new(0.5, 0.5, 0.5));
let blue = materials.add(Color::srgb_u8(124, 144, 255));
Expand Down
2 changes: 2 additions & 0 deletions examples/animation/custom_skinned_mesh.rs
Expand Up @@ -123,6 +123,8 @@ fn setup(

let mesh = meshes.add(mesh);

// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut rng = ChaCha8Rng::seed_from_u64(42);

for i in -5..5 {
Expand Down
2 changes: 2 additions & 0 deletions examples/async_tasks/external_source_external_thread.rs
Expand Up @@ -27,6 +27,8 @@ fn setup(mut commands: Commands) {

let (tx, rx) = bounded::<u32>(10);
std::thread::spawn(move || {
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
loop {
// Everything here happens in another thread
Expand Down
2 changes: 2 additions & 0 deletions examples/ecs/iter_combinations.rs
Expand Up @@ -45,6 +45,8 @@ fn generate_bodies(
let color_range = 0.5..1.0;
let vel_range = -0.5..0.5;

// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
for _ in 0..NUM_BODIES {
let radius: f32 = rng.gen_range(0.1..0.7);
Expand Down
3 changes: 3 additions & 0 deletions examples/ecs/parallel_query.rs
Expand Up @@ -11,6 +11,9 @@ struct Velocity(Vec2);
fn spawn_system(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
let texture = asset_server.load("branding/icon.png");

// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
for _ in 0..128 {
commands.spawn((
Expand Down
3 changes: 2 additions & 1 deletion examples/games/alien_cake_addict.rs
Expand Up @@ -110,7 +110,8 @@ fn setup_cameras(mut commands: Commands, mut game: ResMut<Game>) {

fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMut<Game>) {
let mut rng = if std::env::var("GITHUB_ACTIONS") == Ok("true".to_string()) {
// Make the game play out the same way every time, this is useful for testing purposes.
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
ChaCha8Rng::seed_from_u64(19878367467713)
} else {
ChaCha8Rng::from_entropy()
Expand Down
2 changes: 2 additions & 0 deletions examples/gizmos/axes.rs
Expand Up @@ -42,6 +42,8 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);

// Lights...
Expand Down
8 changes: 8 additions & 0 deletions examples/stress_tests/bevymark.rs
Expand Up @@ -222,6 +222,8 @@ fn setup(
quad: meshes
.add(Rectangle::from_size(Vec2::splat(BIRD_TEXTURE_SIZE as f32)))
.into(),
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
color_rng: ChaCha8Rng::seed_from_u64(42),
material_rng: ChaCha8Rng::seed_from_u64(42),
velocity_rng: ChaCha8Rng::seed_from_u64(42),
Expand Down Expand Up @@ -305,6 +307,8 @@ fn mouse_handler(
mut wave: Local<usize>,
) {
if rng.is_none() {
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
*rng = Some(ChaCha8Rng::seed_from_u64(42));
}
let rng = rng.as_mut().unwrap();
Expand Down Expand Up @@ -538,6 +542,8 @@ fn counter_system(
}

fn init_textures(textures: &mut Vec<Handle<Image>>, args: &Args, images: &mut Assets<Image>) {
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut color_rng = ChaCha8Rng::seed_from_u64(42);
while textures.len() < args.material_texture_count {
let pixel = [color_rng.gen(), color_rng.gen(), color_rng.gen(), 255];
Expand Down Expand Up @@ -573,6 +579,8 @@ fn init_materials(
texture: textures.first().cloned(),
}));

// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut color_rng = ChaCha8Rng::seed_from_u64(42);
let mut texture_rng = ChaCha8Rng::seed_from_u64(42);
materials.extend(
Expand Down
6 changes: 6 additions & 0 deletions examples/stress_tests/many_cubes.rs
Expand Up @@ -124,6 +124,8 @@ fn setup(
let material_textures = init_textures(args, images);
let materials = init_materials(args, &material_textures, material_assets);

// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut material_rng = ChaCha8Rng::seed_from_u64(42);
match args.layout {
Layout::Sphere => {
Expand Down Expand Up @@ -203,6 +205,8 @@ fn setup(
}

fn init_textures(args: &Args, images: &mut Assets<Image>) -> Vec<Handle<Image>> {
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut color_rng = ChaCha8Rng::seed_from_u64(42);
let color_bytes: Vec<u8> = (0..(args.material_texture_count * 4))
.map(|i| if (i % 4) == 3 { 255 } else { color_rng.gen() })
Expand Down Expand Up @@ -247,6 +251,8 @@ fn init_materials(
..default()
}));

// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut color_rng = ChaCha8Rng::seed_from_u64(42);
let mut texture_rng = ChaCha8Rng::seed_from_u64(42);
materials.extend(
Expand Down
2 changes: 2 additions & 0 deletions examples/transforms/align.rs
Expand Up @@ -54,6 +54,8 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut seeded_rng = ChaCha8Rng::seed_from_u64(19878367467712);

// A camera looking at the origin
Expand Down
2 changes: 2 additions & 0 deletions examples/ui/font_atlas_debug.rs
Expand Up @@ -105,5 +105,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut state: ResM
},
));
});
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
commands.insert_resource(SeededRng(ChaCha8Rng::seed_from_u64(19878367467713)));
}

0 comments on commit d39ab55

Please sign in to comment.