Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB and C# gernerate random number array

Status
Not open for further replies.

cappmgr

Programmer
Jan 29, 2003
639
US
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
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
 
In C#, you use the Length to allocate an array.
Int32[] retSequence = new Int32[totalNumberRuns];

In VB, you use the UpperBound to allocate an array
Dim retSequence(totalNumberRuns-1) As Int32



- free online Compare/Diff of snippets
 
Thank you very much John.

After making the array as you stated
Dim retSequence(totalNumberRuns-1) As Int32
and changing
Code:
retSequence((totalNumberRuns - j - 1)) = CInt(al(index)) / numberRuns
to
Code:
retSequence((totalNumberRuns - j - 2)) = Convert.ToInt32(Floor(Convert.ToDouble((al(index))) / numberRuns))
It does what I want.

Here is what I find funny. I removed the cast from the C# code and changed it to Convert.ToInt32 and got the same results as using (int)(al[index]). I removed the CInt from the VB code and replaced it with Convert.ToInt32 and still got the unwanted 3's. To finally get the results I wanted from VB I had to make it a Double then get the Floor and then make it an Int32. Why didn't the Convert.ToInt32 work the same in VB?

Marty
 
Change
retSequence((totalNumberRuns - j - 2)) = Convert.ToInt32(Floor(Convert.ToDouble((al(index))) / numberRuns))
to
retSequence((totalNumberRuns - j - 2)) = Convert.ToInt32(Math.Floor(al(index) / numberRuns))

ToInt32 rounds.
ToInt32
"Return Value
value rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4, and 5.5 is converted to 6"


- free online Compare/Diff of snippets
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top