SRL Archive - C#/Visual Basic Help and Tutorials

The random function


mariofan12

2008-10-05 12:24:31

Yes visual basic does have functions like SCAR and it does return a result…
Well here's my random function

If you want to use it in your program, then paste this at the top of your code.

Public function Random(ByVal Range as integer, ByVal Min as integer) As Integer

Dim randomsin as single = Rnd() * Range 'Randomizes the single.
randomsin = randomsin + Min 'Will as the min you used to add to the random int.
return int(randomsin) 'Makes the single a whole number and returns it

End Function

How to use:
Lets say you want to a label called "label1" to display a random number between 10 and 15 (so the range is 5 and the min is 10).

label1.text = Random(5, 10)

see how that works?
This can be used for many different things like case statements or games.

TViYH

2008-10-05 23:24:09

You spelled function wrong in Public function =]

mariofan12

2008-10-06 23:21:08

wow… ill be fixing that lol

Wrycu

2008-10-07 16:08:41


Public function Random(ByVal Range as integer, ByVal Min as integer) As Integer

Randomize 'Randomizes the order in which random numbers are picked
Dim randomsin as single = Rnd() * Range 'Randomizes the single.
randomsin = randomsin + Min 'Will as the min you used to add to the random int.

return int(randomsin) 'Makes the single a whole number and returns it

'or
random = randomsin

End Function

Fixed.

Or.

Public Function Random(upperlimit as Integer, Optional Min1 as Integer = 1) as Integer

Randomize
Random = int(rnd(upperlimit) + Min1)

End Function