I am trying to generate an array of numbers randomly. The numbers are either 0,1 or 2. Works fine for my C# test stub but for the life of me I can not figure out how to make it work in VB. The array returned in VB is 1 longer than the C# and the VB generats 0,1,2 or 3.
aRandom Length: 300
iZero: 100
iOne: 100
iTwo: 100
iWhat: 0
VB results
aRandom Length: 301
iZero: 52
iOne: 99
iTwo: 101
iWhat: 49
C# code
VB code
Any help greatly appreciated,
Marty
aRandom Length: 300
iZero: 100
iOne: 100
iTwo: 100
iWhat: 0
VB results
aRandom Length: 301
iZero: 52
iOne: 99
iTwo: 101
iWhat: 49
C# code
Code:
using System;
using System.Collections;
class MainClass
{
public static void Main(string[] args)
{
MainClass sp = new MainClass();
Int32[] aRandom;
//Int32 numberTestCases = 3;
//Int32 totalNumberRuns = 100;
int iZero, iOne, iTwo, iWhat;
iZero = iOne = iTwo = iWhat = 0;
Console.WriteLine("start GetRandomSequence:");
aRandom = sp.GetRandomSequence(3, 100);
for (int i = 0; i < aRandom.Length; ++i )
{
Console.WriteLine("aRandom Number: " + aRandom[i]);
switch (aRandom[i])
{
case 0:
{
iZero +=1;
break;
}
case 1:
{
iOne +=1;
break;
}
case 2:
{
iTwo +=1;
break;
}
default:
{
iWhat +=1;
break;
}
}
}
Console.WriteLine("aRandom Length: " + aRandom.Length.ToString());
Console.WriteLine("iZero: " + iZero);
Console.WriteLine("iOne: " + iOne);
Console.WriteLine("iTwo: " + iTwo);
Console.WriteLine("iWhat: " + iWhat);
Console.ReadLine();
}
public Int32[] GetRandomSequence(Int32 numberTestCases, Int32 numberRuns)
{
Int32 totalNumberRuns = numberRuns * numberTestCases;
ArrayList al = new ArrayList(totalNumberRuns);
Int32[] retSequence = new Int32[totalNumberRuns];
for (int i = 0; i < totalNumberRuns; ++i){
al.Add(i);
}
Random rand = new Random();
for (int j = totalNumberRuns - 1; j != -1; --j){
Int32 index = rand.Next(0, j+1);
retSequence[totalNumberRuns-j-1] = (int)al[index] / numberRuns;
al.RemoveAt(index);
}
return retSequence;
}
}
VB code
Code:
Imports System
Imports System.Collections
Class MainClass
Public Shared Sub Main(args() As String)
Dim sp As New MainClass()
Dim aRandom() As Int32
'Int32 numberTestCases = 3;
'Int32 totalNumberRuns = 100;
Dim iZero, iOne, iTwo, iWhat As Integer
iZero = 0
iOne = 0
iTwo = 0
iWhat = 0
Console.WriteLine("start GetRandomSequence:")
aRandom = sp.GetRandomSequence(3, 100)
Dim i As Int32
For i = 0 to aRandom.Length - 1
Console.WriteLine(("aRandom Number: " & aRandom(i).ToString))
Select Case aRandom(i)
Case 0
iZero += 1
Case 1
iOne += 1
Case 2
iTwo += 1
Case Else
iWhat += 1
End Select
Next i
Console.WriteLine(("aRandom Length: " & aRandom.Length.ToString))
Console.WriteLine(("iZero: " & iZero.ToString))
Console.WriteLine(("iOne: " & iOne.ToString))
Console.WriteLine(("iTwo: " & iTwo.ToString))
Console.WriteLine(("iWhat: " & iWhat.ToString))
Console.ReadLine()
End Sub
Public Function GetRandomSequence(numberTestCases As Int32, numberRuns As Int32) As Int32()
Dim totalNumberRuns As Int32 = numberRuns * numberTestCases
Dim al As New ArrayList(totalNumberRuns)
Dim retSequence(totalNumberRuns) As Int32
Dim i As Integer
For i = 0 to totalNumberRuns - 1
al.Add(i)
Next i
Dim rand As New Random()
Dim j As Integer
j = totalNumberRuns - 1
While j <> - 1
j = j - 1
Dim index As Int32 = rand.Next(0, j + 1)
retSequence((totalNumberRuns - j - 1)) = CInt(al(index)) / numberRuns
al.RemoveAt(index)
End While
Return retSequence
End Function 'GetRandomSequence
End Class 'MainClass
Any help greatly appreciated,
Marty