Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By JohnH

Thread: Seeding a Random Number

  1. #1
    Bryan_James is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2015
    Location
    Manila, Philippines, Asia, Earth
    Posts
    5
    Reputation
    0

    Seeding a Random Number

    Hi!
    I am new to VB.NET. I want to seed a random number but I don't know how. Is there any something like "srand (time(nulll))" counter part it VB.NET? Thanks in advance!

  2. #2
    JohnH's Avatar
    JohnH is online now VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,085
    Reputation
    2803
    Bryan_James and lhdvbdot like this.
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  3. #3
    Solitaire is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    465
    Reputation
    210
    Random is a class within the System namespace. It is also an object which must be created with the New keyword.
    Random() without an argument is based on the system timer, which is constantly changing. It is left blank for games of chance. To repeat the same sequence of random numbers, place a seed number, which can be any integer, inside the ().

    Dim randomvariable As New Random(seednumber)

  4. #4
    Bryan_James is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2015
    Location
    Manila, Philippines, Asia, Earth
    Posts
    5
    Reputation
    0
    Thanks for your helps! But how can I randomize an integer number from 1 to 9? I did it previously with Rnd() function. But after changing the datatype of the variables from integer to New Random () I can't use the Rnd() function anymore? How can I do these?
    Last edited by Bryan_James; 04-15-2015 at 7:58 AM.

  5. #5
    Solitaire is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    465
    Reputation
    210
    The range of the random number is established with the Next() method, which takes two arguments - the starting number, and the ending number + 1. In order to include the last number in the range, you must add 1 to the ending number. The value of the first argument must be less than the value of the second. The randomly selected number must also be declared. Example:

    Dim randvar As New Random()
    Dim num As Integer
    num = randvar.Next(first, last + 1)
    Console.WriteLine(num)

    The number one before last is included in the range. To select a number between 1-10, the last argument must be 11.
    For example, to select a number between 1 and 9 you would do:
    num = randvar.Next(1, 10)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •