Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8319767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:25:38+00:00 2026-06-08T22:25:38+00:00

Dim top = RandomPosition() Dim left = RandomPosition() End Sub Function RandomPosition() Dim rand

  • 0
Dim top = RandomPosition()
Dim left = RandomPosition()    
End Sub

Function RandomPosition()
    Dim rand As New Random()
    Dim number = rand.Next(1, 100)
    Return number
End Function

Hi guys I am trying to get 2 different random values (for now. Once this works I will need a few more). The problem is that with the above code top and left always equal the same random number.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:25:40+00:00Added an answer on June 8, 2026 at 10:25 pm

    You create a new random sequence every time you call RandomPosition but, because you’re calling it in quick succession, they’ll have the same seed (based on time). Same seed means same sequence.

    You should create the rand variable once, then just continue to use it, something like:

    Dim rand as New Random()
    Dim top = rand.Next (1, 100)
    Dim left = rand.Next (1, 100)
    

    Alternatively, if you really want it in its own function, make the random generator static so that it maintains its state across calls:

    Function RandomPosition()
        Static rand = New Random()
        Return rand.Next(1, 100)
    End Function
    

    The following complete VB2010 program shows this in action:

    Module Module1
        Function RandomPosition()
            Static rand As Random = New Random()
            Return rand.Next(1, 100)
        End Function
    
        Sub Main()
            Dim top = RandomPosition()
            Dim left = RandomPosition()
            MsgBox("top = " & CStr(top) & ", left = " & CStr(left))
        End Sub
    End Module
    

    It outputs, on various runs:

    top = 7, left = 93
    top = 45, left = 90
    top = 44, left = 62
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I return a top 5 with rank number using linq? Dim Top5
I have this sql string: Dim sqlQuery As String = SELECT TOP 1 ID,
Dim classCodeDetails As List(Of ClassCodeDetail) = db.ClassCodeHeaders.Single(Function(cch) cch.CLCH_ID = classCodeHeaderId ).ClassCodeDetails.ToList() classCodeDetails.Sort(Function(c1, c2) c1.Make.MAKE_English.CompareTo(c2.Make.MAKE_English)
Dim objMail As New Mail.MailMessage(no-reply@mywebsite.com, ToEmail, Password Reset, body) ...and the problem is that
dim dataType as String toolTip=Marks And Number[String] I want to get the [String] alone.
Consider the following code: Dim Working As Boolean = False Private Sub TreeView1_AfterCheck(ByVal sender
Here's the code i'm using to rotate: Dim m As New System.Drawing.Drawing2D.Matrix Dim size
Here's my current Macro Public Module CopyrightCode Sub AddCopyrightHeader() Dim doc As Document Dim
Dim arrS(1000, 6) As String Sub FromHere() '(I've already filled the array and just
I'm using VB.net to have a GUI on top of a commandline program. Dim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.