473,411 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,411 developers and data experts.

Generating Random Numbers in VB.NET

SammyB
807 Expert 512MB
These are some "random" thoughts about generating random numbers in Visual Basic.

Wikipedia will give a better introduction than I, see http://en.wikipedia.org/wiki/Random_number_generator.

The key point is that you need to start the random number generator with a seed. Doing it in the form load with the number of milliseconds since midnight is an easy way to start.

The VB.Net version

This demo project will be as simple as I can make it. It consists of a form with one button. Every time you push the button, a MsgBox will pop-up with a random number between 1 and 10. This code is for VB.NET and was created with VB.NET 2005 Express Edition.

Start a new project and double click the button in the toolbox to add a button to the form. Double-click the button to see the code outline:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3.     End Sub
  4. End Class
Replace that code with:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private oRand As Random
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         oRand = New Random(DateTime.Now.Millisecond)
  5.     End Sub
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         Dim iRand As Integer
  8.         iRand = oRand.Next(1, 10)
  9.         MsgBox(iRand)
  10.     End Sub
  11. End Class
Notice the following key items:
  1. oRand is the Random number Generator and is defined at the module level
  2. oRand is initialized once in the Form Load event using the millisecong component of the current time.
  3. The Next method of the Random object generates a new number.
May 4 '07 #1
0 25854

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
2
by: Mike P | last post by:
I have a method that I am using to generate random numbers using Random.Next. However, every time I call the method I get the same random number. Is there a C# equivalent of Randomize is VB6? ...
10
by: Glenn Wilson | last post by:
I have a quick Question and I Hope some one can help or at least explain. What is happening is that I am trying to use random numbers in an application, as per the sample test code below. When I...
5
by: dav3 | last post by:
I have almost completed a monster assignment on sorting algorithms (quick, insertion and selection) using c++ but I am lost on one part of the assignment. I have to generate a random list of numbers...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
8
by: kiranchahar | last post by:
Hey all, How do I generate random numbers with Uniform distribution Uniform(a,b) using C-programming? I want to generate uniform random numbers which have mean following Uniform(p,q) and also...
2
by: enrique21 | last post by:
Im trying to generate random numbers using the statement: Random r = new Random(2); for (int i=1;i<=18;i++) but im having errors in my results here is my entire code so far: import...
12
by: 9966 | last post by:
Greetings, This is my second post till now. Thanks for all the advice given to me for the first post. Now I'm having problem with generating random numbers. I know if we want to generate a...
3
by: diggity | last post by:
okay, so i started on code for Sudoku on ready to program java ... very close to java. I was able to create a 2d array and display and change the variables. What i want to do now is to generate...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.