Results 1 to 6 of 6

Thread: C sharp program to print random numbers

  1. #1
    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. #2
    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:
    using System;

    class CSHRPProgram

    {
    static void Main(string args[])

    {
    Random RandmNum = new Random();

    int k = RandmNum.Next();

    Console.WriteLine(k);

    Console.Read();
    }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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.
    private int RandNumber(int m, int n, )
    {
    Random rm = new Random();

    return rm.Next(m, m);

    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: C sharp program to print random numbers

    Have you tried following C sharp code for random number?
    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. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: C sharp program to print random numbers

    C sharp program for random numbers:
    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. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: C sharp program to print random numbers

    Try something below in you random number program of C sharp:
    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++;

    }

Similar Threads

  1. Program to print GCD of given numbers
    By Bhavesh_seth in forum Software Development
    Replies: 5
    Last Post: 15-03-2011, 08:03 PM
  2. C Program to print Combination of numbers
    By Prashobh Mallu in forum Software Development
    Replies: 6
    Last Post: 26-03-2010, 09:21 PM
  3. Need C program code to count numbers of negative & positive numbers
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 02:00 PM
  4. Random Numbers in C
    By Rob Dizzle in forum Software Development
    Replies: 5
    Last Post: 30-12-2009, 10:37 AM
  5. Program to print odd and even numbers
    By roodiii in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 02:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,501,038.83133 seconds with 17 queries