This program generates Unique Random Numbers and displays them on the console.
Editor Used
Antechinus, http://www.c-point.com.
Debugger
dbgurt.exe
Thanks Saurabh for help and guidance and thanks Mahesh for loading my tutorial. And of course thanks to you for reading it -- you know who you are!
- class testrandom {
- publicstaticvoid 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) {
-
- Console.WriteLine("Enter value greater than or equal to 1");
- return;
- }
- }
- unique_random generateit = new unique_random();
- generateit.create_random(intno);
- }
- }
- class unique_random {
- publicvoid create_random(int passed_intno) {
-
- int LowerBound = 1;
- int UpperBound = passed_intno;
- bool firsttime = true;
- int starti = 0;
- int[] vararray;
- vararray = newint[UpperBound];
-
- Random randomGenerator = new Random(DateTime.Now.Millisecond);
- do {
- int nogenerated = randomGenerator.Next(LowerBound, UpperBound + 1);
-
-
- if (firsttime)
- {
- vararray[starti] = nogenerated;
- firsttime = false;
- starti++;
- } else
- {
- bool duplicate_flag = CheckDuplicate(nogenerated, starti, vararray);
- if (!duplicate_flag)
- {
- vararray[starti] = nogenerated;
- starti++;
- }
- }
- }
- while (starti < UpperBound);
- PrintArray(vararray);
- }
- publicbool 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;
- }
-
- publicstaticvoid PrintArray(Array arr) {
- Console.Write("{");
- int count = 0;
- int li = arr.Length;
- foreach(object o in arr) {
- Console.Write("{0}", o);
- count++;
-
- if (count < li) Console.Write(", ");
- }
- Console.WriteLine("}");
- }
- }