PDF Generating Random Numbers in Visual Basic - CSC 111
Generating Random Numbers in Visual Basic:
Random number generators are used for all types of applications, including testing, simulation, and games.
Pseudo-random numbers can be generated in Visual Basic through the use of the rnd() function:
Module Module1
Sub Main() ' Generate ten random values:
Console.WriteLine("Ten Random values: ") Console.WriteLine()
For i = 1 To 10 Dim rValue = Rnd() Console.WriteLine("
Next
" & rValue)
Console.WriteLine() Console.Write("Hit enter to exit the program...") Dim dummyValue = Console.ReadLine() End Sub
End Module
Sample run:
Ten Random values:
0.7055475 0.533424 0.5795186 0.2895625 0.301948 0.7747401 0.01401764 0.7607236 0.81449 0.7090379
Hit enter to exit the program...
Notes: -
The rnd() function returns pseudo-random values between 0 ? 1, exclusive o These Random values are uniformly distributed across the range from 0 ? 1, i.e. the number of values less than ? will equal the number of values that are equal to ? , but less than 1, over the long run.
- If the program is run a second time, the output/random values generated are the same ? try it! o Why? The equation used does not change... o To change the runs so that each value is different, use the Randomize() function...
Module Module1
Sub Main() ' Generate ten random values:
Randomize() ' Generate a different sequence of numbers each time the program is run
Console.WriteLine("Ten Random values: ") Console.WriteLine()
For i = 1 To 10 Dim rValue = Rnd() Console.WriteLine("
Next
" & rValue)
Console.WriteLine() Console.Write("Hit enter to exit the program...") Dim dummyValue = Console.ReadLine() End Sub
End Module
Sample run: Ten Random values:
0.847851 0.3252636 0.5477498 0.3536189 0.2035288 0.7887477 0.8157449 0.8242917 0.2175357 0.7186509
Hit enter to exit the program...
A simple program to simulate tossing a coin 100 times that outputs the number of heads and tails tossed:
Module Module1
Sub Main() ' Coin tossing
Randomize() ' Generate a different sequence of numbers each time the program is run
Console.WriteLine("Simulate tossing a coin 100 times: ") Console.WriteLine()
Dim heads As Integer = 0 Dim tails As Integer = 0
For i = 1 To 100 Dim rValue = Rnd()
If (rValue < 0.5) Then heads = heads + 1
Else tails = tails + 1
End If Next
' 0 - 0.5, exclusive => heads
Console.WriteLine("Number of heads: " & heads) Console.WriteLine("Number of tails: " & tails)
Console.WriteLine() Console.Write("Hit enter to exit the program...") Dim dummyValue = Console.ReadLine() End Sub
End Module
Sample runs: Simulate tossing a coin 100 times: Number of heads: 56 Number of tails: 44 Hit enter to exit the program...
Simulate tossing a coin 100 times: Number of heads: 45 Number of tails: 55 Hit enter to exit the program...
Simulate tossing a coin 100 times: Number of heads: 53 Number of tails: 47 Hit enter to exit the program... Finally to generate random values over a given range use:
randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound
More on this next week...
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- pdf passing an array to and from a function or subprocedure in vb6
- pdf arrays in visual basic vb net
- pdf visual basic quick reference cheat sheet
- pdf variable declaration in vb
- pdf generating random numbers the rand function
- pdf visual basic objects and collections ucl
- pdf using loops to repeat code
- pdf visual basic for excel university of michigan
- pdf console student booklet st crispin s
- pdf data types arithmetic strings input
Related searches
- visual basic codes for excel
- visual basic for excel examples
- visual basic for beginners excel
- excel visual basic programming examples
- visual basic examples for beginners
- microsoft visual basic for excel
- excel visual basic tutorial pdf
- visual basic for beginners pdf
- microsoft visual basic tutorial pdf
- visual basic programs with codes
- visual basic book pdf download
- visual basic programming for beginners