locked
Ranom and Randomize RRS feed

  • Question

  • User-829096311 posted

    Hello
    This code from Visual Basic script

    Randomize(10)
    MsgBox (rnd())
    

    always returns same value
    but when I running the code in VB.NET  gives me random value
    I need to run this code in VB.NET and  returns same value.

    Thank you

    Sunday, August 31, 2014 6:58 PM

Answers

  • User-760709272 posted
    Random r = new Random(10);
    int x = r.Next();
    // or
    int x = r.Next(100); // where 100 is the max value etc

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 31, 2014 7:08 PM

All replies

  • User-760709272 posted
    Random r = new Random(10);
    int x = r.Next();
    // or
    int x = r.Next(100); // where 100 is the max value etc

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 31, 2014 7:08 PM
  • User-829096311 posted

    the code work, but what if we want to using rnd to generate random values then we want to stop random in this point to save this value

    like

    Randomize(1245)

    then i can pause the value change

    and can retreve all randome value by using one value Randomaze(5825)

    Color.FromArgb(255 * Rnd(), 255 * Rnd(), 255 * Rnd(), 255 * Rnd())
    Graphics.FillRectangle(RandomeBrush, 0, 0, 100, 100)
    
    

     this code gives all color not just gray RGB(42,42,42)

    Sunday, August 31, 2014 8:23 PM
  • User-829096311 posted

    Hello
    This code from Visual Basic script

    Randomize(10)
    MsgBox (rnd())

    always returns same value
    but when I running the code in VB.NET  gives me random value
    I need to run this code in VB.NET and  returns same value.


    this is an example

    Randomize(1245)
    Color.FromArgb(255 * Rnd(), 255 * Rnd(), 255 * Rnd(), 255 * Rnd())
    Graphics.FillRectangle(RandomeBrush, 0, 0, 100, 100)
    

    then i can call the color from its randome value
    THANK YOU

    Wednesday, September 3, 2014 6:30 PM
  • User465171450 posted

    What do you mean you need it to return the same value? That's the entire point of the Rnd() is to not return the same value. The rnd() in VBScript, like other older languages suffered from issues whereby it would generate the same sequence of random numbers each time it started up. That's not what random should ever do and now the randomize functions are seeded to help avoid this.

    Wednesday, September 3, 2014 6:51 PM
  • User-760709272 posted

    When you use an explicit seed as in his code, you *will* get the same numbers each time, as random number generators are just mathematical functions that act on the seed and seem random but aren't.  When you create an instance of Random with no explicit seed it uses DateTime.Now.Ticks as the seed, so as long as you don't create new Random classes in very quick succession you'll get your pseudo random numbers back.

    I gave this person the code he needs on some other thread already though.

    Wednesday, September 3, 2014 7:31 PM