locked
random values RRS feed

  • Question

  • I am using silverlight vb.net

    I have a class which creates a set of classes.
    class A -> creates of 10 class B


    Each of class B has a a few random numbers for a calculation.
    The issue is the random numbers are not random as most are similar. I dont want to pass a whole lot of random numbers to the class B to make it work.

    Thursday, August 19, 2010 3:21 AM

Answers

  • No. Like I said: "make your Random a static field of class B". You're still generating the values in class B. However I think the problem is that at the moment you're using a Random object for every instance of the class, which can make the generated random numbers very similar. If you only use one single Random object (e.g. a static field) for that, the results will be much better.

    Edit: like that:

    class B
    {
      private static Random _rnd = new Random();
    
      ...
    }


    Thursday, August 19, 2010 5:01 AM

All replies

  • Hi. I assume you're creating a new "Random" object in each class B? So a solution would be to make your Random a static field of class B, so it's only created (and seeded!) once. If that is not the problem (or doesn't solve it), may I ask if you can post the code of your random number generation?

    Thursday, August 19, 2010 3:34 AM
  • you mean i create the random variable in class A and pass that to class B?

     

     

    Thursday, August 19, 2010 3:42 AM
  • No. Like I said: "make your Random a static field of class B". You're still generating the values in class B. However I think the problem is that at the moment you're using a Random object for every instance of the class, which can make the generated random numbers very similar. If you only use one single Random object (e.g. a static field) for that, the results will be much better.

    Edit: like that:

    class B
    {
      private static Random _rnd = new Random();
    
      ...
    }


    Thursday, August 19, 2010 5:01 AM