Random Number Generator

Forum Home Forums Understanding the Software Random Number Generator

Tagged: 

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #3716
    eamonng94
    Participant

    Me and my colleague are wondering if there is any possible way of implementing a random number generator with set minimum and max values. I would presume this would be created through a formula in the administrator application?

    Thanks for any help in advance.

    #3717
    Mikhail
    Moderator

    Yes. You should use Random class.
    In the Formulas table add a variable:
    Random rand = new Random();

    Then use it in an input channel, for example:
    rand.NextDouble()

    #3719
    eamonng94
    Participant

    Hi Mikhail,

    this is the formula i have inputted into the formulas tab:

    public double GetRandomNumber(double minimum, double maximum)
    { 
        Random random = new Random();
        return random.NextDouble() * (maximum - minimum) + minimum;
    }

    However one problem i have found is that the numbers are coming from the same seed, so if i call a random number between the same values on two different input channels, it outputs the same number. Is there any way of implementing a formula, which for every instance of the source code, generates its own set of random numbers, different to that of another channel?

    #3722
    eamonng94
    Participant

    Solved the seeding issue by removing the randomizer from the time loop, looks something like this:

    Random random = new Random();
    public double GetRandomNumber(double minimum, double maximum)
    {
       return random.NextDouble() * (maximum - minimum) + minimum;
    }
    • This reply was modified 5 years, 10 months ago by eamonng94.
    • This reply was modified 5 years, 10 months ago by eamonng94.
    #3729
    Mikhail
    Moderator

    Yes, you right.
    Thank you.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.