Table of Contents

9.5 Random number functions

In many theoretical investigations, states, changes of state or values of defined process variables are simulated, as for example in investigations on queues. Random events are required for these simulations, which are realised with random numbers in selected intervals, which are generated with random generators. In the examples supplied with Gambas, there is the project Database/Database, in which several thousand random data are used.

A random generator does not deliver real numbers that are distributed chaotically, but numbers that are calculated according to a certain algorithm. However, this also means that such generators require initial values on the one hand and generate only real pseudo-random numbers on the other.

9.5.1 Initialisation

Syntax:	RANDOMIZE [ Seed AS Integer ] with seed as an optional parameter

The random number generator can be initialised with a freely selectable integer starting value if the optional parameter is set to a fixed integer value when Randomize is called. The following approaches can be used:

If you do not use the optional parameter, the timestamp is set for Seed, otherwise the integer value of Seed is used.

9.5.2 Random numbers

You can also use a random number generator in Gambas, which in the default configuration will give you random numbers in the range 0 random number < 1, initialised by the current timestamp.

9.5.2.1 Random numbers in interval 0 Random number < 1

DIM fRandomnumber AS Float
  Randomize
  fRandomnumber = Rnd()

9.5.2.2 Random numbers in interval 0 Random number < Rmin

DIM fRandomnumber AS Float
  Randomize
  fRandomnumber = Rnd(3)

9.5.2.3 Random numbers in interval Rmin Random number < Rmax

DIM fRandomnumber AS Float
  Randomize
  fRandomnumber = Rnd(-2,+2)

9.5.2.4 Integer random numbers in interval Ru Random number Ro

DIM fRandomnumber As Float
DIM iUpperlimit, iLowerlimit As Integer
  Randomize
  fRandomnumber = Int(Rnd(iLowerlimit, iUpperlimit + 1))

9.5.3 Examples

The following examples demonstrate the use of the 2 random number functions in different use cases.

Example 1.

In the first example, random numbers are generated using the random number functions Rnd and Randomize. The effect of specifying a starting value is demonstrated. The source code is short and is therefore given in full:

[1] ' Gambas class file
[2]
[3] Public Sub Form_Open()
[4]   FMain.Center
[5]   FMain.Resizable = False
[6] End '  Form_Open()
[7]
[8] Public Sub btnRnd1_Click()
[9]   Dim fRandomnumber As Float
[10]   Dim iCount As Integer
[11]
[12]   TextArea1.Clear
[13]   Randomize ' optional, because Standard
[14]   For iCount = 1 To 10
[15]       If iCount < 10 Then
[16]           TextArea1.Insert(iCount & gb.Tab & Rnd() & gb.NewLine)
[17]       Else
[18]          TextArea1.Insert(iCount & gb.Tab & Rnd())
[19]       Endif
[20]   Next ' iCount
[21] End ' Rnd1
[22]
[23] Public Sub btnRnd2_Click()
[24]   Dim fRandomnumber As Float
[25]   Dim iCount As Integer
[26]
[27]   TextArea1.Clear
[28]   For iCount = 1 To 10
[29]       Randomize
[30]       If iCount < 10 Then
[31]           TextArea1.Insert(iCount & gb.Tab & Rnd() & gb.NewLine)
[32]       Else
[33]          TextArea1.Insert(iCount & gb.Tab & Rnd())
[34]       Endif
[35]   Next ' iCount
[36] End ' Rnd2
[37]
[38] Public Sub btnRnd3_Click()
[39]   Dim fRandomnumber As Float
[40]   Dim iCount As Integer
[41]
[42]   TextArea1.Clear
[43]   Randomize -2
[44]   For iCount = 1 To 10
[45]       If iCount < 10 Then
[46]           TextArea1.Insert(iCount & gb.Tab & Rnd() & gb.NewLine)
[47]       Else
[48]          TextArea1.Insert(iCount & gb.Tab & Rnd())
[49]       Endif
[50]   Next ' iCount
[51] End ' Rnd3
[52]
[53] Public Sub btnClose_Click()
[54]   FMain.Close
[55] End ' Close

Liste mit Zufallszahlen
Figure 9.5.3.1: List with 10 different random numbers.

Results:

The procedure in lines 8-22 always returns a list of 10 different random numbers. The second procedure in 23-36 only returns random numbers that can contain series of the same numbers - in the extreme case, all random numbers are the same. The reason is that the random number generator has the same starting value for one second. The random numbers in the 3rd procedure in 41-58 are different, but the list of random numbers generated is always the same because the same starting value is always set for the random generator.

Example 2

For testing purposes, one often needs random data that does not necessarily have to be numbers. In the second example, random data of the data type Boolean, Float, Integer, String and Date are needed for the test for sorting data in a GridView.

In the following lines, only the procedure for generating the random data is presented:

Private hSort As New CSort
Private iCount As Integer
 
Public Sub btnFillGrid_Click()
  Dim i As Integer
  Dim vMatrix As Variant[]
  Dim aStrs As String[] = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"]
 
  Randomize
 
  For i = 0 To iCount Step 1
      vMatrix = New Variant[]
      vMatrix.Add(Round(Rnd(0, 10), -2)) ' Real number
      vMatrix.Add(CBool(Round(Rnd(0, 1)))) ' Truth value
      vMatrix.Add(aStrs[CInt(Rnd(0, aStrs.Count))]) ' Character string
      vMatrix.Add(CDate(Rnd(CFloat(Now()), CFloat(Now() + 1000)))) ' Date 1
      vMatrix.Add(CInt(Rnd(-10, 10))) ' Integer number
      vMatrix.Add(CDate(Rnd(CFloat(Now()), CFloat(Now() + 2000)))) ' Date 2
      hSort.Add(vMatrix)
  Next ' i
  ArrayToGrid()
 
End ' FillGrid

Zufallsdaten
Figure 9.5.3.2: GridView with randomly generated records, sorted by the 1st column.

The complete projects for the first two examples are provided in the download section.

Example 3

At http://de.wikibooks.org/wiki/Gambas:_Zufall#Zufallsquadrate you will find a small project where both the coordinates of the squares to be drawn and their colour are calculated randomly.

9.5.4 Download

Projects

Download