Bluefinger / bevy_turborand

A plugin to enable random number generation for the Bevy game engine.
Apache License 2.0
34 stars 5 forks source link

Floating point support #19

Open vladinator1000 opened 1 month ago

vladinator1000 commented 1 month ago

Hi there, just trying the library and it seems f32 doesn't accept ranges?

fn spawn_asteroid(
    mut commands: Commands,
    mut spawn_timer: ResMut<SpawnTimer>,
    time: Res<Time>,
    asset_server: Res<AssetServer>,
    mut global_rng: ResMut<GlobalRng>,
) {
    spawn_timer.timer.tick(time.delta());
    if !spawn_timer.timer.just_finished() {
        return;
    }

    let mut rng = RngComponent::from(&mut global_rng);
    let x = rng.f32(-25.0..25.0);
    let translation = Vec3::new(x, 0.0, 0.0);
}

Getting this error

this method takes 0 arguments but 1 argument was supplied

When I try using u32 it also seems to not support ranges.

let x = rng.u32(-25..25);

the trait bound `u32: Neg` is not satisfied the following other types implement trait `Neg`: &f128 &f16 &f32 &f64 &i128 &i16 &i32 &i64 and 12 others

How would you generate floating point numbers from arbitrary ranges? I would love to be able to do this

    let x = rng.f32(-25.0..25.0);
Bluefinger commented 1 month ago

The f32 and f32_normalized methods don't accept ranges (this is modelled from the fastrand API). This is something I'd need to look at in more detail for turborand, but there's suggestions here for working around this: https://github.com/Bluefinger/turborand/issues/37

If the API surface for bevy_turborand is insufficient for your needs, do consider https://github.com/Bluefinger/bevy_rand as an alternative (configure it via feature flags to use WyRand if you want to use the same fast PRNG algorithm as bevy_turborand.