Computer Science Canada

Giving Random Values to an Array

Author:  DifinityRJ [ Sun Mar 16, 2008 4:28 pm ]
Post subject:  Giving Random Values to an Array

I am having problems with the syntax in Visual basic to give an array that holds 10 values to assign it 10 random numbers.

In Turing you can do it like this

code:
var x :array 1..10 of int
for a:1..10
x(a):=Rand.Int(1,10)
end for


In visual basic...so far I've got

Dim xArray(9) as Integer

Thanks for any help, in advance.

Author:  A.J [ Sun Mar 16, 2008 4:30 pm ]
Post subject:  Re: Giving Random Values to an Array

I'll advise you to check out the tutorials.
They're pretty helpful Wink

Author:  DifinityRJ [ Sun Mar 16, 2008 4:37 pm ]
Post subject:  Re: Giving Random Values to an Array

Yes I've checked them out.

Author:  nike52 [ Sun Mar 16, 2008 6:32 pm ]
Post subject:  Re: Giving Random Values to an Array

stick for loop/while loop here with counter i, going from 0 to 8
xArray(i) = Int((HighestValue - LowestValue + 1) * Rnd) + LowestValue

stick randomize in the form_load to get diff random sequences

i like the bunnie, i have 2 fwuffy bunnies with me right now:D

Author:  CodeMonkey2000 [ Sun Mar 16, 2008 8:20 pm ]
Post subject:  RE:Giving Random Values to an Array

Here is a useful function that is similar to turing's Rand.Int.
code:
Private Function randInt(ByVal x As Integer, ByVal Y As Integer)
   If x > Y Then
      Error (3)
   End If
   randInt = Int(Rnd * (Y - x + 1)) + x
End Function

Author:  unoho [ Mon Sep 15, 2008 11:02 pm ]
Post subject:  RE:Giving Random Values to an Array

hey man, im pretty new to arrays.. but i think it would be something like this??

Dim intX(1 to 10) as integer
dim intCount as integer

for intCount = 1 to 10
intX(intCount) = randint(1,10)
next intCount


im not relaly sure about the randint part because i haven't used it for a long time. lol
just try tht.

Author:  jbking [ Fri Sep 19, 2008 2:37 pm ]
Post subject:  RE:Giving Random Values to an Array

Dim xArray(9) as Integer defines a 9 element array as 0-8 are the valid indices for the array.

Try this instead:
Dim xArray(10)

JB


: