using System;
class testrandom
{
public static void Main(string[] args)
{
int intno;
if (args.Length == 0)
{
Console.WriteLine("Please enter a parameter eg. unique 5");
return;
}
else
{
intno = Int32.Parse(args[0]);
if (intno < 1)
{
// Check to see if user has entered value >= 1
// because my LowerBound is hardcoded to 1
Console.WriteLine("Enter value greater than or equal to 1");
return;
}
}
unique_random generateit = new unique_random();
generateit.create_random(intno);
}
}
class unique_random
{
public void create_random(int passed_intno)
{
int LowerBound = 1;
int UpperBound = passed_intno;
bool firsttime = true;
int starti = 0;
int[] vararray;
vararray = new int[UpperBound];
// Random Class used here
Random randomGenerator = new Random(DateTime.Now.Millisecond);
do
{
int nogenerated = randomGenerator.Next(LowerBound, UpperBound + 1);
// Note: randomGenerator.Next generates no. to UpperBound - 1 hence +1
// i got stuck at this pt & had to use the debugger.
if (firsttime) // if (firsttime == true)
{
vararray[starti] = nogenerated;
// we simply store the nogenerated in vararray
firsttime = false;
starti++;
}
else // if (firsttime == false)
{
bool duplicate_flag = CheckDuplicate(nogenerated, starti, vararray);
// call to check in array
if (!duplicate_flag) // duplicate_flag == false
{
vararray[starti] = nogenerated;
starti++;
}
}
} while (starti < UpperBound);
PrintArray(vararray); // Print the array
}
public bool CheckDuplicate(int newrandomNum, int loopcount, int[] function_array)
{
bool temp_duplicate = false;
for (int j = 0; j < loopcount; j++)
{
if (function_array[j] == newrandomNum)
{
temp_duplicate = true;
break;
}
}
return temp_duplicate;
}
// Print Array
public static void PrintArray(Array arr)
{
Console.Write("{");
int count = 0;
int li = arr.Length;
foreach (object o in arr)
{
Console.Write("{0}", o);
count++;
//Condition to check whether ',' should be added in printing arrray
if (count < li)
Console.Write(", ");
}
Console.WriteLine("}");
}
}
Loading

Manish TewatiaPosted Oct 4, 2010, 12:20 AM
Hi, David by mistake i was post this in VB forms, well can u help me out in if its convert into vb language like:
Class testrandom
Shared Sub Main(ByVal args As String())
Dim intno As Integer
If args.Length = 0 Then
Console.WriteLine("Please enter a parameter eg. unique 5")
Return
Else
intno = Int32.Parse(args(0))
If intno < 1 Then
' Check to see if user has entered value >= 1
' because my LowerBound is hardcoded to 1
Console.WriteLine("Enter value greater than or equal to 1")
Return
End If
End If
Dim generateit As New unique_random()
generateit.create_random(intno)
End Sub
End Class
Class unique_random
Public Sub create_random(ByVal passed_intno As Integer)
Dim LowerBound As Integer = 1
Dim UpperBound As Integer = passed_intno
Dim firsttime As Boolean = True
Dim starti As Integer = 0
Dim vararray As Integer()
vararray = New Integer(UpperBound - 1) {}
' Random Class used here
Dim randomGenerator As New Random(DateTime.Now.Millisecond)
Do
Dim nogenerated As Integer = randomGenerator.[Next](LowerBound, UpperBound + 1)
' Note: randomGenerator.Next generates no. to UpperBound - 1 hence +1
' i got stuck at this pt & had to use the debugger.
If firsttime Then
' if (firsttime == true)
vararray(starti) = nogenerated
' we simply store the nogenerated in vararray
firsttime = False
starti += 1
Else
' if (firsttime == false)
Dim duplicate_flag As Boolean = CheckDuplicate(nogenerated, starti, vararray)
' call to check in array
If Not duplicate_flag Then
' duplicate_flag == false
vararray(starti) = nogenerated
starti += 1
End If
End If
Loop While starti < UpperBound
PrintArray(vararray)
' Print the array
End Sub
Public Function CheckDuplicate(ByVal newrandomNum As Integer, ByVal loopcount As Integer, ByVal function_array As Integer()) As Boolean
Dim temp_duplicate As Boolean = False
For j As Integer = 0 To loopcount - 1
If function_array(j) = newrandomNum Then
temp_duplicate = True
Exit For
End If
Next
Return temp_duplicate
End Function
' Print Array
Shared Sub PrintArray(ByVal arr As Array)
Console.Write("{")
Dim count As Integer = 0
Dim li As Integer = arr.Length
For Each o As Object In arr
Console.Write("{0}", o)
count += 1
'Condition to check whether ',' should be added in printing arrray
If count < li Then
Console.Write(", ")
End If
Next
Console.WriteLine("}")
End Sub
End Class
Thank you...
David CarterPosted Oct 3, 2010, 1:32 AM