Tutorial - Random Numbers [VB6 and earlier]
Many questions in VB are regarding random numbers. In this tutorial I will try and explain the basic ideas regarding random numbers so it'll be easier for you to create your applications.
Intro - The Rnd Function
The first thing you need to know is the function that generates a random number. The function is called : Rnd. Here is how you use it :
1) Open a new project in VB.
2) In the load event of your form add the following code :
VB Code:
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 10
Debug.Print Rnd
Next
End Sub
3) Run the project. In the Immediate window you will notice that 10 numbers have been printed.
Re: Tutorial - Random Numbers
Hi all,
I am new, to this forum and also to VB.Net and I encountered a problem. I need an array that contains 100 random numbers, between 1 and 100. More specifically, the array must contain all the numbers between 1 and 100, but in random order, and I don't know how to do that. Any help would be appreciated.
Thank you.
Re: Tutorial - Random Numbers
Welcome to VBForums :wave:
This tutorial is actually for Classic VB, not VB.Net.
You should ask in our VB.Net forum, as the methods are different.
Re: Tutorial - Random Numbers
Thank you,
I will post it on the VB.Net forum, as you suggested.
Re: Tutorial - Random Numbers
Pls Anyone Help Me Here, I Dont Get The Resultz On The Explanation Above
http://i37.tinypic.com/2cpuvsi.jpg
Re: Tutorial - Random Numbers
Welcome to VBForums :wave:
As the code uses Debug.Print, the numbers will be printed to the Immediate window - if it isn't visible, select it on the View menu.
Re: Tutorial - Random Numbers
Your Random Number tutorial is extremly helpful so firstly id like to thank you for posting it.
Would it be possible to change the above code (1st option) so that you could call a list of random numbers one at a time using a command button?
Thanks again
Robert Stevenson
Re: Tutorial - Random Numbers
Hi,
This is really very useful for the new vb programmers.
Re: Tutorial - Random Numbers
Dim randomnumber As New Random
MessageBox.Show(randomnumber.Next())
only number between 1 and 9
Dim randomnumber As New Random
MessageBox.Show(randomnumber.Next(1,9))
Re: Tutorial - Random Numbers
I don't think that is VB6.
Visual Basic.NET and Classic Visual Basic are separated on this forum.
Take a look at the forum index and learn about all the different categories.
Re: Tutorial - Random Numbers
Quote:
Originally Posted by
TheBigB
I don't think that is VB6.
Visual Basic.NET and Classic Visual Basic are separated on this forum.
Take a look at the
forum index and learn about all the different categories.
It's not it's vb.net! ruipedromachado should have said so in his post. Although, seeing as there is no specification in the original post or the thread title regarding which Visual Basic version is being talked about I can see why he got confused.
Re: Tutorial - Random Numbers [VB6 and earlier]
I've added VB6 to the thread title to save that kind of confusion in future.
Re: Tutorial - Random Numbers [VB6 and earlier]
how is these possible?
the pc can random, always, the same number... in diferent executions... can anyone explain to me why?
Re: Tutorial - Random Numbers [VB6 and earlier]
You need to use the Randomize statement as shown in post #2.
If you want different numbers each time, call Randomize just once at the start of your program (eg: in Form_Load).
If you want to be able to repeat the same set of numbers, call Rnd with a negative argument first, and then Randomize with whatever number you want, eg:
Code:
Rnd -1
Randomize 2345