1

I am using the rgsl library in Rust that wraps functions from the C GSL math libraries. I was using a random number generator function, but I am always getting the same exact value whenever I generate a new random number. I imagine that the number should vary upon each run of the function. Is there something that I am missing? Do I need to set a new random seed each time or such?

extern crate rgsl;

use rgsl::Rng;

fn main() {
    rgsl::RngType::env_setup();

    let t = rgsl::rng::default();
    let r = Rng::new(&t).unwrap()

    let val = rgsl::randist::binomial::binomial(&r, 0.01f64, 1u32);
    print!("{}",val);
}

The value I keep getting is 1, which seems really high considering the probability of obtaining a 1 is 0.01.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
krishnab
  • 9,270
  • 12
  • 66
  • 123

1 Answers1

2

The documentation for env_setup explains everything you need to know:

This function reads the environment variables GSL_RNG_TYPE and GSL_RNG_SEED and uses their values to set the corresponding library variables gsl_rng_default and gsl_rng_default_seed

If you don’t specify a generator for GSL_RNG_TYPE then gsl_rng_mt19937 is used as the default. The initial value of gsl_rng_default_seed is zero.

(Emphasis mine)

Like all software random number generators, this is really an algorithm that produces pseudo random numbers. The algorithm and the initial seed uniquely identify a sequence of these numbers. Since the seed is always the same, the first (and second, third, ...) number in the sequence will always be the same.

So if I want to generate a new series of random numbers, then I need to change the seed each time. However, if I use the rng to generate a set of random seeds, then I will get the same seeds each time.

That's correct.

Other languages don't seem to have this constraint, meaning that the seed can be manually set if desired, but is otherwise is random.

A classical way to do this is to seed your RNG with the current time. This produces an "acceptable" seed for many cases. You can also get access to true random data from the operating system and use that as a seed or mix it in to produce more random data.

Is there no way to do this in Rust?

This is a very different question. If you just want a random number generator in Rust, use the rand crate. This uses techniques like I described above.

You could even do something crazy like using random values from the rand crate to seed your other random number generator. I just assumed that there is some important reason you are using that crate instead of rand.

Community
  • 1
  • 1
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Thanks for the note. I figured it was using the same seed. So I looked at the documentation a bit closer and one thing is confusing. So if I want to generate a new series of random numbers, then I need to change the seed each time. However, if I use the rng to generate a set of random seeds, then I will get the same seeds each time. So I can never really get away from the deterministic side of the pseudo random generator. Other languages don't seem to have this constraint, meaning that the seed can be manually set if desired, but is otherwise is random. Is there no way to do this in Rust? – krishnab Oct 05 '15 at 01:36
  • Oh thanks @Shepmaster. Yep, I have to use the rgsl crate because it has a function for generating binomial random variables. This is really helpful though. I get how this works now. – krishnab Oct 05 '15 at 02:16