Skip to content

Commit b09f3bd

Browse files
authored
Switch to portable RNG in examples (#12644)
# Objective Fixes issue #12613 - the RNG used in examples is _deterministic_, but its implementation is not _portable_ across platforms. We want to switch to using a portable RNG that does not vary across platforms, to ensure certain examples play out the same way every time. ## Solution Replace all occurences of `rand::rngs::StdRng` with `rand_chacha::ChaCha8Rng`, as recommended in issue #12613 --- ## Changelog - Add `rand_chacha` as a new dependency (controversial?) - Replace all occurences of `rand::rngs::StdRng` with `rand_chacha::ChaCha8Rng`
1 parent 92535b4 commit b09f3bd

File tree

12 files changed

+55
-43
lines changed

12 files changed

+55
-43
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ bevy_internal = { path = "crates/bevy_internal", version = "0.14.0-dev", default
330330

331331
[dev-dependencies]
332332
rand = "0.8.0"
333+
rand_chacha = "0.3.1"
333334
ron = "0.8.0"
334335
flate2 = "1.0"
335336
serde = { version = "1", features = ["derive"] }

examples/3d/spotlight.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use bevy::{
77
pbr::NotShadowCaster,
88
prelude::*,
99
};
10-
use rand::{rngs::StdRng, Rng, SeedableRng};
10+
use rand::{Rng, SeedableRng};
11+
use rand_chacha::ChaCha8Rng;
1112

1213
const INSTRUCTIONS: &str = "\
1314
Controls
@@ -48,7 +49,7 @@ fn setup(
4849
));
4950

5051
// cubes
51-
let mut rng = StdRng::seed_from_u64(19878367467713);
52+
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
5253
let cube_mesh = meshes.add(Cuboid::new(0.5, 0.5, 0.5));
5354
let blue = materials.add(Color::srgb_u8(124, 144, 255));
5455

examples/animation/custom_skinned_mesh.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use bevy::{
1414
render_asset::RenderAssetUsages,
1515
},
1616
};
17-
use rand::{rngs::StdRng, Rng, SeedableRng};
17+
use rand::{Rng, SeedableRng};
18+
use rand_chacha::ChaCha8Rng;
1819

1920
fn main() {
2021
App::new()
@@ -122,7 +123,7 @@ fn setup(
122123

123124
let mesh = meshes.add(mesh);
124125

125-
let mut rng = StdRng::seed_from_u64(42);
126+
let mut rng = ChaCha8Rng::seed_from_u64(42);
126127

127128
for i in -5..5 {
128129
// Create joint entities

examples/async_tasks/external_source_external_thread.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use bevy::prelude::*;
44
// Using crossbeam_channel instead of std as std `Receiver` is `!Sync`
55
use crossbeam_channel::{bounded, Receiver};
6-
use rand::{rngs::StdRng, Rng, SeedableRng};
6+
use rand::{Rng, SeedableRng};
7+
use rand_chacha::ChaCha8Rng;
78
use std::time::{Duration, Instant};
89

910
fn main() {
@@ -26,7 +27,7 @@ fn setup(mut commands: Commands) {
2627

2728
let (tx, rx) = bounded::<u32>(10);
2829
std::thread::spawn(move || {
29-
let mut rng = StdRng::seed_from_u64(19878367467713);
30+
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
3031
loop {
3132
// Everything here happens in another thread
3233
// This is where you could connect to an external data source

examples/ecs/iter_combinations.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Shows how to iterate over combinations of query results.
22
33
use bevy::{color::palettes::css::ORANGE_RED, prelude::*};
4-
use rand::{rngs::StdRng, Rng, SeedableRng};
4+
use rand::{Rng, SeedableRng};
5+
use rand_chacha::ChaCha8Rng;
56

67
fn main() {
78
App::new()
@@ -44,7 +45,7 @@ fn generate_bodies(
4445
let color_range = 0.5..1.0;
4546
let vel_range = -0.5..0.5;
4647

47-
let mut rng = StdRng::seed_from_u64(19878367467713);
48+
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
4849
for _ in 0..NUM_BODIES {
4950
let radius: f32 = rng.gen_range(0.1..0.7);
5051
let mass_value = radius.powi(3) * 10.;

examples/ecs/parallel_query.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
33
use bevy::ecs::query::BatchingStrategy;
44
use bevy::prelude::*;
5-
use rand::{rngs::StdRng, Rng, SeedableRng};
5+
use rand::{Rng, SeedableRng};
6+
use rand_chacha::ChaCha8Rng;
67

78
#[derive(Component, Deref)]
89
struct Velocity(Vec2);
910

1011
fn spawn_system(mut commands: Commands, asset_server: Res<AssetServer>) {
1112
commands.spawn(Camera2dBundle::default());
1213
let texture = asset_server.load("branding/icon.png");
13-
let mut rng = StdRng::seed_from_u64(19878367467713);
14+
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
1415
for _ in 0..128 {
1516
commands.spawn((
1617
SpriteBundle {

examples/games/alien_cake_addict.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use std::f32::consts::PI;
44

55
use bevy::prelude::*;
6-
use rand::{rngs::StdRng, Rng, SeedableRng};
6+
use rand::{Rng, SeedableRng};
7+
use rand_chacha::ChaCha8Rng;
78

89
#[derive(Clone, Eq, PartialEq, Debug, Hash, Default, States)]
910
enum GameState {
@@ -82,7 +83,7 @@ struct Game {
8283
}
8384

8485
#[derive(Resource, Deref, DerefMut)]
85-
struct Random(StdRng);
86+
struct Random(ChaCha8Rng);
8687

8788
const BOARD_SIZE_I: usize = 14;
8889
const BOARD_SIZE_J: usize = 21;
@@ -110,9 +111,9 @@ fn setup_cameras(mut commands: Commands, mut game: ResMut<Game>) {
110111
fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut game: ResMut<Game>) {
111112
let mut rng = if std::env::var("GITHUB_ACTIONS") == Ok("true".to_string()) {
112113
// Make the game play out the same way every time, this is useful for testing purposes.
113-
StdRng::seed_from_u64(19878367467713)
114+
ChaCha8Rng::seed_from_u64(19878367467713)
114115
} else {
115-
StdRng::from_entropy()
116+
ChaCha8Rng::from_entropy()
116117
};
117118

118119
// reset the game state

examples/gizmos/axes.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! This example demonstrates the implementation and behavior of the axes gizmo.
22
use bevy::prelude::*;
33
use bevy::render::primitives::Aabb;
4-
use rand::{rngs::StdRng, Rng, SeedableRng};
4+
use rand::{Rng, SeedableRng};
5+
use rand_chacha::ChaCha8Rng;
56
use std::f32::consts::PI;
67

78
const TRANSITION_DURATION: f32 = 2.0;
@@ -34,14 +35,14 @@ struct TransformTracking {
3435
}
3536

3637
#[derive(Resource)]
37-
struct SeededRng(StdRng);
38+
struct SeededRng(ChaCha8Rng);
3839

3940
fn setup(
4041
mut commands: Commands,
4142
mut meshes: ResMut<Assets<Mesh>>,
4243
mut materials: ResMut<Assets<StandardMaterial>>,
4344
) {
44-
let mut rng = StdRng::seed_from_u64(19878367467713);
45+
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
4546

4647
// Lights...
4748
commands.spawn(PointLightBundle {

examples/stress_tests/bevymark.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use bevy::{
1818
window::{PresentMode, WindowResolution},
1919
winit::{UpdateMode, WinitSettings},
2020
};
21-
use rand::{rngs::StdRng, seq::SliceRandom, Rng, SeedableRng};
21+
use rand::{seq::SliceRandom, Rng, SeedableRng};
22+
use rand_chacha::ChaCha8Rng;
2223

2324
const BIRDS_PER_SECOND: u32 = 10000;
2425
const GRAVITY: f32 = -9.8 * 100.0;
@@ -181,10 +182,10 @@ struct BirdResources {
181182
textures: Vec<Handle<Image>>,
182183
materials: Vec<Handle<ColorMaterial>>,
183184
quad: Mesh2dHandle,
184-
color_rng: StdRng,
185-
material_rng: StdRng,
186-
velocity_rng: StdRng,
187-
transform_rng: StdRng,
185+
color_rng: ChaCha8Rng,
186+
material_rng: ChaCha8Rng,
187+
velocity_rng: ChaCha8Rng,
188+
transform_rng: ChaCha8Rng,
188189
}
189190

190191
#[derive(Component)]
@@ -221,10 +222,10 @@ fn setup(
221222
quad: meshes
222223
.add(Rectangle::from_size(Vec2::splat(BIRD_TEXTURE_SIZE as f32)))
223224
.into(),
224-
color_rng: StdRng::seed_from_u64(42),
225-
material_rng: StdRng::seed_from_u64(42),
226-
velocity_rng: StdRng::seed_from_u64(42),
227-
transform_rng: StdRng::seed_from_u64(42),
225+
color_rng: ChaCha8Rng::seed_from_u64(42),
226+
material_rng: ChaCha8Rng::seed_from_u64(42),
227+
velocity_rng: ChaCha8Rng::seed_from_u64(42),
228+
transform_rng: ChaCha8Rng::seed_from_u64(42),
228229
};
229230

230231
let text_section = move |color: Srgba, value: &str| {
@@ -300,11 +301,11 @@ fn mouse_handler(
300301
windows: Query<&Window>,
301302
bird_resources: ResMut<BirdResources>,
302303
mut counter: ResMut<BevyCounter>,
303-
mut rng: Local<Option<StdRng>>,
304+
mut rng: Local<Option<ChaCha8Rng>>,
304305
mut wave: Local<usize>,
305306
) {
306307
if rng.is_none() {
307-
*rng = Some(StdRng::seed_from_u64(42));
308+
*rng = Some(ChaCha8Rng::seed_from_u64(42));
308309
}
309310
let rng = rng.as_mut().unwrap();
310311
let window = windows.single();
@@ -332,7 +333,7 @@ fn mouse_handler(
332333
fn bird_velocity_transform(
333334
half_extents: Vec2,
334335
mut translation: Vec3,
335-
velocity_rng: &mut StdRng,
336+
velocity_rng: &mut ChaCha8Rng,
336337
waves: Option<usize>,
337338
dt: f32,
338339
) -> (Transform, Vec3) {
@@ -537,7 +538,7 @@ fn counter_system(
537538
}
538539

539540
fn init_textures(textures: &mut Vec<Handle<Image>>, args: &Args, images: &mut Assets<Image>) {
540-
let mut color_rng = StdRng::seed_from_u64(42);
541+
let mut color_rng = ChaCha8Rng::seed_from_u64(42);
541542
while textures.len() < args.material_texture_count {
542543
let pixel = [color_rng.gen(), color_rng.gen(), color_rng.gen(), 255];
543544
textures.push(images.add(Image::new_fill(
@@ -572,8 +573,8 @@ fn init_materials(
572573
texture: textures.first().cloned(),
573574
}));
574575

575-
let mut color_rng = StdRng::seed_from_u64(42);
576-
let mut texture_rng = StdRng::seed_from_u64(42);
576+
let mut color_rng = ChaCha8Rng::seed_from_u64(42);
577+
let mut texture_rng = ChaCha8Rng::seed_from_u64(42);
577578
materials.extend(
578579
std::iter::repeat_with(|| {
579580
assets.add(ColorMaterial {

examples/stress_tests/many_cubes.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use bevy::{
2323
window::{PresentMode, WindowResolution},
2424
winit::{UpdateMode, WinitSettings},
2525
};
26-
use rand::{rngs::StdRng, seq::SliceRandom, Rng, SeedableRng};
26+
use rand::{seq::SliceRandom, Rng, SeedableRng};
27+
use rand_chacha::ChaCha8Rng;
2728

2829
#[derive(FromArgs, Resource)]
2930
/// `many_cubes` stress test
@@ -123,7 +124,7 @@ fn setup(
123124
let material_textures = init_textures(args, images);
124125
let materials = init_materials(args, &material_textures, material_assets);
125126

126-
let mut material_rng = StdRng::seed_from_u64(42);
127+
let mut material_rng = ChaCha8Rng::seed_from_u64(42);
127128
match args.layout {
128129
Layout::Sphere => {
129130
// NOTE: This pattern is good for testing performance of culling as it provides roughly
@@ -202,7 +203,7 @@ fn setup(
202203
}
203204

204205
fn init_textures(args: &Args, images: &mut Assets<Image>) -> Vec<Handle<Image>> {
205-
let mut color_rng = StdRng::seed_from_u64(42);
206+
let mut color_rng = ChaCha8Rng::seed_from_u64(42);
206207
let color_bytes: Vec<u8> = (0..(args.material_texture_count * 4))
207208
.map(|i| if (i % 4) == 3 { 255 } else { color_rng.gen() })
208209
.collect();
@@ -246,8 +247,8 @@ fn init_materials(
246247
..default()
247248
}));
248249

249-
let mut color_rng = StdRng::seed_from_u64(42);
250-
let mut texture_rng = StdRng::seed_from_u64(42);
250+
let mut color_rng = ChaCha8Rng::seed_from_u64(42);
251+
let mut texture_rng = ChaCha8Rng::seed_from_u64(42);
251252
materials.extend(
252253
std::iter::repeat_with(|| {
253254
assets.add(StandardMaterial {

0 commit comments

Comments
 (0)