TechArena Community C sharp program to print random numbers

#1
12-01-2010
 Member Join Date: Dec 2009 Posts: 48
C sharp program to print random numbers

Hi All,

I am new in the field of C sharp programming. That why I am getting too much difficulties while coding simple C sharp programs. I have to write the C sharp program to print the Random number. I have tried one code logic, but I am getting too much errors during it's code execution. Is anyone has the C sharp code to print the random number then please share with me. I would greatly appreciate your help over this C sharp program.
#2
12-01-2010
 Member Join Date: Apr 2008 Posts: 1,948
Re: C sharp program to print random numbers

Hi,

Please refer the following C sharp program which prints the random number:
Quote:
 using System; class CSHRPProgram { static void Main(string args[]) { Random RandmNum = new Random(); int k = RandmNum.Next(); Console.WriteLine(k); Console.Read(); } }
#3
12-01-2010
 Member Join Date: May 2008 Posts: 2,008
Re: C sharp program to print random numbers

Hi friend,

You need to use the below mentioned method in your C sharp program for random number. Here I used the 'RandNumber()' method to print eh random number. Then I defined the 'rm' variable as a random variable to calculate random number.
Quote:
 private int RandNumber(int m, int n, ) { Random rm = new Random(); return rm.Next(m, m); }
#4
12-01-2010
 Member Join Date: Apr 2008 Posts: 2,001
Re: C sharp program to print random numbers

Have you tried following C sharp code for random number?
Quote:
 bool [] nmbers = new bool[10]; Random rd = new Random(); int hs = 0; while(hs < 5) { int ps = rd.Next(9); if (nmbers[pos] == true) continue; nmbers[pos] = true; hs++; } Console.WriteLine("Random Numbers : "); for(int a=0; a<10; a++) { if (nmbers[a] == true) Console.Write("{0} ", a.ToString()); } Console.WriteLine();
#5
12-01-2010
 Member Join Date: May 2008 Posts: 2,293
Re: C sharp program to print random numbers

C sharp program for random numbers:
Quote:
 using System; using System.Collections.Generic; using System.Text; namespace CSRAD { class Pm { static void Main(string[] args) { int k = 0; int l= 0; } } class CSDEMO { private static int n; public static int N { get { return n; } } static CSDEMO() { Random rf = new Random(); n = rf.Next(); } } }
#6
12-01-2010
 Member Join Date: Oct 2005 Posts: 2,389
Re: C sharp program to print random numbers

Try something below in you random number program of C sharp:
Quote:
 Random r = new Random(); bool z = new bool [10]; int c = 0; while(c < 10) { int pos = r.Next(9); if (z[pos] == true) continue; z[pos] = true; c++; }