Adding explanation to seeded rng used in examples (#12593)

# Objective

- Fixes #12544

## Solution

- Added/updated a universally worded comment to all seeded rng instances
in our examples.
This commit is contained in:
andristarr 2024-03-26 20:40:18 +01:00 committed by GitHub
parent b7ab1466c7
commit d39ab55b61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 34 additions and 1 deletions

View file

@ -49,6 +49,9 @@ fn setup(
)); ));
// cubes // 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 mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
let cube_mesh = meshes.add(Cuboid::new(0.5, 0.5, 0.5)); let cube_mesh = meshes.add(Cuboid::new(0.5, 0.5, 0.5));
let blue = materials.add(Color::srgb_u8(124, 144, 255)); let blue = materials.add(Color::srgb_u8(124, 144, 255));

View file

@ -123,6 +123,8 @@ fn setup(
let mesh = meshes.add(mesh); 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); let mut rng = ChaCha8Rng::seed_from_u64(42);
for i in -5..5 { for i in -5..5 {

View file

@ -27,6 +27,8 @@ fn setup(mut commands: Commands) {
let (tx, rx) = bounded::<u32>(10); let (tx, rx) = bounded::<u32>(10);
std::thread::spawn(move || { 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); let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
loop { loop {
// Everything here happens in another thread // Everything here happens in another thread

View file

@ -45,6 +45,8 @@ fn generate_bodies(
let color_range = 0.5..1.0; let color_range = 0.5..1.0;
let vel_range = -0.5..0.5; 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); let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
for _ in 0..NUM_BODIES { for _ in 0..NUM_BODIES {
let radius: f32 = rng.gen_range(0.1..0.7); let radius: f32 = rng.gen_range(0.1..0.7);

View file

@ -11,6 +11,9 @@ struct Velocity(Vec2);
fn spawn_system(mut commands: Commands, asset_server: Res<AssetServer>) { fn spawn_system(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default()); commands.spawn(Camera2dBundle::default());
let texture = asset_server.load("branding/icon.png"); 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); let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
for _ in 0..128 { for _ in 0..128 {
commands.spawn(( commands.spawn((

View file

@ -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>) { 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()) { 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) ChaCha8Rng::seed_from_u64(19878367467713)
} else { } else {
ChaCha8Rng::from_entropy() ChaCha8Rng::from_entropy()

View file

@ -42,6 +42,8 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, 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); let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
// Lights... // Lights...

View file

@ -222,6 +222,8 @@ fn setup(
quad: meshes quad: meshes
.add(Rectangle::from_size(Vec2::splat(BIRD_TEXTURE_SIZE as f32))) .add(Rectangle::from_size(Vec2::splat(BIRD_TEXTURE_SIZE as f32)))
.into(), .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), color_rng: ChaCha8Rng::seed_from_u64(42),
material_rng: ChaCha8Rng::seed_from_u64(42), material_rng: ChaCha8Rng::seed_from_u64(42),
velocity_rng: ChaCha8Rng::seed_from_u64(42), velocity_rng: ChaCha8Rng::seed_from_u64(42),
@ -305,6 +307,8 @@ fn mouse_handler(
mut wave: Local<usize>, mut wave: Local<usize>,
) { ) {
if rng.is_none() { 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)); *rng = Some(ChaCha8Rng::seed_from_u64(42));
} }
let rng = rng.as_mut().unwrap(); let rng = rng.as_mut().unwrap();
@ -538,6 +542,8 @@ fn counter_system(
} }
fn init_textures(textures: &mut Vec<Handle<Image>>, args: &Args, images: &mut Assets<Image>) { 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); let mut color_rng = ChaCha8Rng::seed_from_u64(42);
while textures.len() < args.material_texture_count { while textures.len() < args.material_texture_count {
let pixel = [color_rng.gen(), color_rng.gen(), color_rng.gen(), 255]; let pixel = [color_rng.gen(), color_rng.gen(), color_rng.gen(), 255];
@ -573,6 +579,8 @@ fn init_materials(
texture: textures.first().cloned(), 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 color_rng = ChaCha8Rng::seed_from_u64(42);
let mut texture_rng = ChaCha8Rng::seed_from_u64(42); let mut texture_rng = ChaCha8Rng::seed_from_u64(42);
materials.extend( materials.extend(

View file

@ -124,6 +124,8 @@ fn setup(
let material_textures = init_textures(args, images); let material_textures = init_textures(args, images);
let materials = init_materials(args, &material_textures, material_assets); 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); let mut material_rng = ChaCha8Rng::seed_from_u64(42);
match args.layout { match args.layout {
Layout::Sphere => { Layout::Sphere => {
@ -203,6 +205,8 @@ fn setup(
} }
fn init_textures(args: &Args, images: &mut Assets<Image>) -> Vec<Handle<Image>> { 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 mut color_rng = ChaCha8Rng::seed_from_u64(42);
let color_bytes: Vec<u8> = (0..(args.material_texture_count * 4)) let color_bytes: Vec<u8> = (0..(args.material_texture_count * 4))
.map(|i| if (i % 4) == 3 { 255 } else { color_rng.gen() }) .map(|i| if (i % 4) == 3 { 255 } else { color_rng.gen() })
@ -247,6 +251,8 @@ fn init_materials(
..default() ..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 color_rng = ChaCha8Rng::seed_from_u64(42);
let mut texture_rng = ChaCha8Rng::seed_from_u64(42); let mut texture_rng = ChaCha8Rng::seed_from_u64(42);
materials.extend( materials.extend(

View file

@ -54,6 +54,8 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, 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); let mut seeded_rng = ChaCha8Rng::seed_from_u64(19878367467712);
// A camera looking at the origin // A camera looking at the origin

View file

@ -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))); commands.insert_resource(SeededRng(ChaCha8Rng::seed_from_u64(19878367467713)));
} }