How to generate random number each time?

How to generate 2 different random numbers in VB?

  • I just need to make two different random numbers that are not the same all the time. i have two different random number generators but they always generate the same number so there is never any variance between the player points, which is what i need. this is the code im using NOTE: mrndOut1 and 2 are declared as modular variables 'player 1 points Dim rndNumber1 As Random rndNumber1 = New Random mrndOut1 = rndNumber1.Next(1, 6) If mrndOut1 = 1 Then lblRnUmber1.Text = "S" End If If mrndOut1 = 2 Then lblRnUmber1.Text = "D" End If If mrndOut1 = 3 Then lblRnUmber1.Text = "R" End If If mrndOut1 = 4 Then lblRnUmber1.Text = "T" End If If mrndOut1 = 5 Then lblRnUmber1.Text = "Y" End If If mrndOut1 = 6 Then lblRnUmber1.Text = "P" End If 'player 2 points Dim rndNumber2 As Random rndNumber2 = New Random mrndOut2 = rndNumber2.Next(1, 6) If mrndOut2 = 1 Then lblRnUmber2.Text = "O" End If If mrndOut2 = 2 Then lblRnUmber2.Text = ("I") End If If mrndOut2 = 3 Then lblRnUmber2.Text = ("G") End If If mrndOut2 = 4 Then lblRnUmber2.Text = "J" End If If mrndOut2 = 5 Then lblRnUmber2.Text = "K" End If If mrndOut2 = 6 Then lblRnUmber2.Text = "L" End If btnGetPoints.Enabled = False thanks for the help

  • Answer:

    Without providing a seed value you will always get the same sequence of numbers when calling next(). This is to make any results repeatable. To get random result you need to use a seed value that changes each time the program runs. You only need on Random object so you should use only one. rndNumber1.Next(1, 6) will get you numers from 1 to 5. To get results from 1 to 6 you should code rndNumber1.Next(1, 7)

Sgt at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

The thing to remember about a random number generator (RNG) is that it uses a mathematical algorithm to produce a sequence of 'random' numbers. Since the algorithm itself is fixed, it will create the exact same 'random' sequence every time unless some semi-random factor is thrown into the mix. That's what's know as 'seeding' the RNG and done by first executing Randomize()

TheMadProfessor

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.