# Array and Random problems

I really appreciate if anyone of you can give me a sample coding about the Array and Random. or you may tell me where to get it... It's much easier for me to learn... I'm a newbie here...

• [b][red]This message was edited by iwilld0it at 2004-10-31 14:14:51[/red][/b][hr]
: I really appreciate if anyone of you can give me a sample coding about the Array and Random. or you may tell me where to get it... It's much easier for me to learn... I'm a newbie here...
:

Here is an example that randomly picks a person from a string array ...

[code]
' Create an array
Dim people(4) As String

' Store people names
people(0) = "Mike"
people(1) = "John"
people(2) = "Jane"
people(3) = "Susan"
people(4) = "Frank"

Dim personIndex As Integer

' This statement makes sure the following statement is truly random
Randomize Timer

' This says to return a random number between 0 and 4 inclusive
personIndex = CInt(Rnd() * UBound(people))

' See what person name was randomly picked
MsgBox people(personIndex)
[/code]

The key statement is ...

[code]
CInt(Rnd() * UBound(people))
[/code]

The Rnd() function returns a number between 0 and 1. Multiplying by UBound(people) (which is 4) effectively says to pick a random number between 0 and 4. Since the Rnd() function returns a number in decimal format, we need to use the CInt() function.

If we failed to add this line ...

[code]
Randomize Timer
[/code]

... then the Rnd() function will keep picking the same sequence of numbers.

Note that you could have used just this statement ...

[code]
Randomize
[/code]

• What you are saying is ALMOST correct, but not entirely:
Rnd returns a number between 0 and 1, including 0, EXCLUDING 1!!!
So to get a random number from 0 to 4 will be:
RandomNumber = Int(Rnd() * 5)
...since 0 to 4 are 5 numbers. To do the same for 1 to 5:
RandomNumber = Int(Rnd() * 5) + 1

It's quite an important difference!

Greets...
Richard

• [b][red]This message was edited by iwilld0it at 2004-11-2 10:14:38[/red][/b][hr]
[b][red]This message was edited by iwilld0it at 2004-11-2 10:13:41[/red][/b][hr]
: What you are saying is ALMOST correct, but not entirely:
: Rnd returns a number between 0 and 1, including 0, EXCLUDING 1!!!
: So to get a random number from 0 to 4 will be:
: RandomNumber = Int(Rnd() * 5)
: ...since 0 to 4 are 5 numbers. To do the same for 1 to 5:
: RandomNumber = Int(Rnd() * 5) + 1
:
: It's quite an important difference!
:
: Greets...
: Richard
:
:

You are correct about the rnd() function returning a value from 0 to 1 not including 1, however here is another point.

In my example I used the CInt() function similar to ...

[code]
CInt(rnd() * 4)
[/code]

Unlike the Int() function, which seems to chop off the decimal, CInt() actually rounds the value properly, which is the reason why my code was working consistently.

In fact I do not even need to use CInt() because storing the result returned from Rnd() in an integer implicitly does the same thing ...

[code]
Dim i As Integer
i = Rnd() * 4
[/code]

Here is the code I used to test my theory ...

[code]
Dim n1 As Single
Dim n2 As Integer
Dim i As Integer

For i = 1 To 25
Randomize Timer
n1 = Rnd() * 4
n2 = n1
Debug.Print n2 & " - " & CInt(n1) & " - " & Int(n1) & " - " & n1
Next
[/code]

• : [b][red]This message was edited by iwilld0it at 2004-11-2 10:14:38[/red][/b][hr]
: [b][red]This message was edited by iwilld0it at 2004-11-2 10:13:41[/red][/b][hr]
: : What you are saying is ALMOST correct, but not entirely:
: : Rnd returns a number between 0 and 1, including 0, EXCLUDING 1!!!
: : So to get a random number from 0 to 4 will be:
: : RandomNumber = Int(Rnd() * 5)
: : ...since 0 to 4 are 5 numbers. To do the same for 1 to 5:
: : RandomNumber = Int(Rnd() * 5) + 1
: :
: : It's quite an important difference!
: :
: : Greets...
: : Richard
: :
: :
:
: You are correct about the rnd() function returning a value from 0 to 1 not including 1, however here is another point.
:
: In my example I used the CInt() function similar to ...
:
: [code]
: CInt(rnd() * 4)
: [/code]
:
: Unlike the Int() function, which seems to chop off the decimal, CInt() actually rounds the value properly, which is the reason why my code was working consistently.
:
: In fact I do not even need to use CInt() because storing the result returned from Rnd() in an integer implicitly does the same thing ...
:
: [code]
: Dim i As Integer
: i = Rnd() * 4
: [/code]
:
: Here is the code I used to test my theory ...
:
: [code]
: Dim n1 As Single
: Dim n2 As Integer
: Dim i As Integer
:
: For i = 1 To 25
: Randomize Timer
: n1 = Rnd() * 4
: n2 = n1
: Debug.Print n2 & " - " & CInt(n1) & " - " & Int(n1) & " - " & n1
: Next
: [/code]
:
:
:
:
:
:

Well... If you round it, the chances won't be fair:
[code]
[b]CInt(Rnd() * 4)[/b]
[italic]Rnd() * 4 CInt() result total[/italic]
0 to 0.49999 will make 0 0.49999
0.5 to 1.49999 will make 1 0.99999
1.5 to 2.49999 will make 2 0.99999
2.5 to 3.49999 will make 3 0.99999
3.5 to 3.99999 will make 4 0.49999
[/code]

And if you look at the Int(Rnd() * 4) it is fair chance:
[code]
0 to 0.99999 0 0.99999
1 to 1.99999 1 0.99999
2 to 2.99999 2 0.99999
3 to 3.99999 3 0.99999
[/code]

That's why you should use Int() and not CInt()

PS: There is something wrong with the Round() function and therefor also the CInt() function which causes some numbers like 1.4999 to be rounded to 2 instead of 1, or something like that. I forgot what the problem was exactly but it's another reason not to use CInt() for such!

Greets...
Richard

• : : [b][red]This message was edited by iwilld0it at 2004-11-2 10:14:38[/red][/b][hr]
: : [b][red]This message was edited by iwilld0it at 2004-11-2 10:13:41[/red][/b][hr]
: : : What you are saying is ALMOST correct, but not entirely:
: : : Rnd returns a number between 0 and 1, including 0, EXCLUDING 1!!!
: : : So to get a random number from 0 to 4 will be:
: : : RandomNumber = Int(Rnd() * 5)
: : : ...since 0 to 4 are 5 numbers. To do the same for 1 to 5:
: : : RandomNumber = Int(Rnd() * 5) + 1
: : :
: : : It's quite an important difference!
: : :
: : : Greets...
: : : Richard
: : :
: : :
: :
: : You are correct about the rnd() function returning a value from 0 to 1 not including 1, however here is another point.
: :
: : In my example I used the CInt() function similar to ...
: :
: : [code]
: : CInt(rnd() * 4)
: : [/code]
: :
: : Unlike the Int() function, which seems to chop off the decimal, CInt() actually rounds the value properly, which is the reason why my code was working consistently.
: :
: : In fact I do not even need to use CInt() because storing the result returned from Rnd() in an integer implicitly does the same thing ...
: :
: : [code]
: : Dim i As Integer
: : i = Rnd() * 4
: : [/code]
: :
: : Here is the code I used to test my theory ...
: :
: : [code]
: : Dim n1 As Single
: : Dim n2 As Integer
: : Dim i As Integer
: :
: : For i = 1 To 25
: : Randomize Timer
: : n1 = Rnd() * 4
: : n2 = n1
: : Debug.Print n2 & " - " & CInt(n1) & " - " & Int(n1) & " - " & n1
: : Next
: : [/code]
: :
: :
: :
: :
: :
: :
:
: Well... If you round it, the chances won't be fair:
: [code]
: [b]CInt(Rnd() * 4)[/b]
: [italic]Rnd() * 4 CInt() result total[/italic]
: 0 to 0.49999 will make 0 0.49999
: 0.5 to 1.49999 will make 1 0.99999
: 1.5 to 2.49999 will make 2 0.99999
: 2.5 to 3.49999 will make 3 0.99999
: 3.5 to 3.99999 will make 4 0.49999
: [/code]
:
: And if you look at the Int(Rnd() * 4) it is fair chance:
: [code]
: 0 to 0.99999 0 0.99999
: 1 to 1.99999 1 0.99999
: 2 to 2.99999 2 0.99999
: 3 to 3.99999 3 0.99999
: [/code]
:
: That's why you should use Int() and not CInt()
:
: PS: There is something wrong with the Round() function and therefor also the CInt() function which causes some numbers like 1.4999 to be rounded to 2 instead of 1, or something like that. I forgot what the problem was exactly but it's another reason not to use CInt() for such!
:
: Greets...
: Richard
:
:

So this is the adjustment that would have to be made then (comparing to my original response) ...

[code]
personIndex = Int(Rnd() * UBound(people) + 1)
[/code]

Correct?

• [b][red]This message was edited by BitByBit_Thor at 2004-11-3 9:17:39[/red][/b][hr]
:
: So this is the adjustment that would have to be made then (comparing to my original response) ...
:
: [code]
: personIndex = Int(Rnd() * UBound(people) + 1)
: [/code]
:
: Correct?
:
:

Almost correct (assuming LBound(...) = 0)
[code]
personIndex = Int(Rnd() * (UBound(people) + 1))
[/code]
Or for any LBound:
[code]
personIndex = Int(Rnd() * (UBound(people) - LBound(people) + 1))
[/code]

Greets...
Richard