From c127f52091fc7a04099558c0c7e1b2d5979bbfdc Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Thu, 22 Apr 2021 10:27:57 +0100 Subject: [PATCH] Boids example: cleaner rand code --- examples/boids/main.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/examples/boids/main.rs b/examples/boids/main.rs index 7048d6fb1..cfd57c23a 100644 --- a/examples/boids/main.rs +++ b/examples/boids/main.rs @@ -1,6 +1,7 @@ // Flocking boids example with gpu compute update pass // adapted from https://github.com/austinEng/webgpu-samples/blob/master/src/examples/computeBoids.ts +use rand::distributions::{Distribution, Uniform}; use std::{borrow::Cow, mem}; use wgpu::util::DeviceExt; @@ -177,12 +178,13 @@ impl framework::Example for Example { // buffer for all particles data of type [(posx,posy,velx,vely),...] let mut initial_particle_data = vec![0.0f32; (4 * NUM_PARTICLES) as usize]; + let mut rng = rand::thread_rng(); + let unif = Uniform::new_inclusive(-1.0, 1.0); for particle_instance_chunk in initial_particle_data.chunks_mut(4) { - particle_instance_chunk[0] = 2.0 * (rand::random::() - 0.5); // posx - particle_instance_chunk[1] = 2.0 * (rand::random::() - 0.5); // posy - particle_instance_chunk[2] = 2.0 * (rand::random::() - 0.5) * 0.1; // velx - particle_instance_chunk[3] = 2.0 * (rand::random::() - 0.5) * 0.1; - // vely + particle_instance_chunk[0] = unif.sample(&mut rng); // posx + particle_instance_chunk[1] = unif.sample(&mut rng); // posy + particle_instance_chunk[2] = unif.sample(&mut rng) * 0.1; // velx + particle_instance_chunk[3] = unif.sample(&mut rng) * 0.1; // vely } // creates two buffers of particle data each of size NUM_PARTICLES