Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding explanation to seeded rng used in examples #12593

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/3d/spotlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ fn setup(
));

// cubes

// Make it deterministic for testing purposes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Make it deterministic 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.

This may need to be reflected on the other examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want this exact wording for all instances? I feel its a little bit verbose.

Copy link
Member

@james7132 james7132 Mar 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, but explaining the full reasoning is probably for the best.

let mut rng = StdRng::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
1 change: 1 addition & 0 deletions examples/animation/custom_skinned_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ fn setup(

let mesh = meshes.add(mesh);

// Make it deterministic for testing purposes.
let mut rng = StdRng::seed_from_u64(42);

for i in -5..5 {
Expand Down
1 change: 1 addition & 0 deletions examples/async_tasks/external_source_external_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn setup(mut commands: Commands) {

let (tx, rx) = bounded::<u32>(10);
std::thread::spawn(move || {
// Make it deterministic for testing purposes.
let mut rng = StdRng::seed_from_u64(19878367467713);
loop {
// Everything here happens in another thread
Expand Down
1 change: 1 addition & 0 deletions examples/ecs/iter_combinations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fn generate_bodies(
let color_range = 0.5..1.0;
let vel_range = -0.5..0.5;

// Make it deterministic for testing purposes.
let mut rng = StdRng::seed_from_u64(19878367467713);
for _ in 0..NUM_BODIES {
let radius: f32 = rng.gen_range(0.1..0.7);
Expand Down
2 changes: 2 additions & 0 deletions examples/ecs/parallel_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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");

// Make it deterministic for testing purposes.
let mut rng = StdRng::seed_from_u64(19878367467713);
for _ in 0..128 {
commands.spawn((
Expand Down
2 changes: 1 addition & 1 deletion examples/games/alien_cake_addict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ 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.
// Make it deterministic for testing purposes.
StdRng::seed_from_u64(19878367467713)
} else {
StdRng::from_entropy()
Expand Down
1 change: 1 addition & 0 deletions examples/gizmos/axes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Make it deterministic for testing purposes.
let mut rng = StdRng::seed_from_u64(19878367467713);

// Lights...
Expand Down
4 changes: 4 additions & 0 deletions examples/stress_tests/bevymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ fn setup(
quad: meshes
.add(Rectangle::from_size(Vec2::splat(BIRD_TEXTURE_SIZE as f32)))
.into(),
// Make it deterministic for testing purposes.
color_rng: StdRng::seed_from_u64(42),
material_rng: StdRng::seed_from_u64(42),
velocity_rng: StdRng::seed_from_u64(42),
Expand Down Expand Up @@ -304,6 +305,7 @@ fn mouse_handler(
mut wave: Local<usize>,
) {
if rng.is_none() {
// Make it deterministic for testing purposes.
*rng = Some(StdRng::seed_from_u64(42));
}
let rng = rng.as_mut().unwrap();
Expand Down Expand Up @@ -537,6 +539,7 @@ fn counter_system(
}

fn init_textures(textures: &mut Vec<Handle<Image>>, args: &Args, images: &mut Assets<Image>) {
// Make it deterministic for testing purposes.
let mut color_rng = StdRng::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 @@ -572,6 +575,7 @@ fn init_materials(
texture: textures.first().cloned(),
}));

// Make it deterministic for testing purposes.
let mut color_rng = StdRng::seed_from_u64(42);
let mut texture_rng = StdRng::seed_from_u64(42);
materials.extend(
Expand Down
3 changes: 3 additions & 0 deletions examples/stress_tests/many_cubes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ fn setup(
let material_textures = init_textures(args, images);
let materials = init_materials(args, &material_textures, material_assets);

// Make it deterministic for testing purposes.
let mut material_rng = StdRng::seed_from_u64(42);
match args.layout {
Layout::Sphere => {
Expand Down Expand Up @@ -202,6 +203,7 @@ fn setup(
}

fn init_textures(args: &Args, images: &mut Assets<Image>) -> Vec<Handle<Image>> {
// Make it deterministic for testing purposes.
let mut color_rng = StdRng::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 @@ -246,6 +248,7 @@ fn init_materials(
..default()
}));

// Make it deterministic for testing purposes.
let mut color_rng = StdRng::seed_from_u64(42);
let mut texture_rng = StdRng::seed_from_u64(42);
materials.extend(
Expand Down
1 change: 1 addition & 0 deletions examples/transforms/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Make it deterministic for testing purposes.
let mut seeded_rng = StdRng::seed_from_u64(19878367467712);

// A camera looking at the origin
Expand Down
1 change: 1 addition & 0 deletions examples/ui/font_atlas_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut state: ResM
},
));
});
// Make it deterministic for testing purposes.
commands.insert_resource(SeededRng(StdRng::seed_from_u64(19878367467713)));
}