locked
How do you create a random number in C#

    Question

  • Hi,

    This may seem a very basic question, but I am having difficulty getting it to work. Could some one please tell me how I can generate a random number, between two given numbers, in C# please.

    Wednesday, November 01, 2006 11:20 AM

Answers

  • Use Random class. You could experiment a little, don't you think...? And please use the MSDN documentation search. Try writing word "Random" to the search field.

    Random integer between 0 and 100 (100 not included):

    Random random = new Random();
    int randomNumber = random.Next(0, 100);
    Wednesday, November 01, 2006 11:53 AM

All replies

  • Use Random class. You could experiment a little, don't you think...? And please use the MSDN documentation search. Try writing word "Random" to the search field.

    Random integer between 0 and 100 (100 not included):

    Random random = new Random();
    int randomNumber = random.Next(0, 100);
    Wednesday, November 01, 2006 11:53 AM
  •  

    i have some code (c below) but i cant get it to show the no. that it has genirated can u help me

     

    Random Ramdom = new Random();

     

    int randomNumber = Ramdom.Next(0, 100);

    card1.Text =

    //code to show the no. hear.


    try SAIO v1.0 go to sites.google.com/site/sameden2
    Wednesday, April 01, 2009 3:06 PM
  •          

    Random random = new Random();
    int randomNumber = random.Next();
    card1.Text = randomNumber.ToString();


    You need to convert the randomNumber type to a string because the Card1.text type is a string.

    • Proposed as answer by aahx Tuesday, October 27, 2009 3:37 PM
    Tuesday, October 27, 2009 3:37 PM
  • You could experiment a little, don't you think...?

    Google   "C#" random number  and  this page is first result.

    Friday, February 17, 2012 9:38 PM
  • People in 2009 were very shortsighted.
    Thursday, March 01, 2012 2:27 AM
  • "experiment a little, don't you think" ... Listen here you pompous jerk, if he had the answer he would not have to beg for help on this page filled with know-it-alls. Believe me, it stinks having to kiss up to all the eggheads for answers. But hey- some of us have jobs that require a lot of time, and we don't have time to troll the knowledgebase for people who don't know what they are doing.... So go done to the corner market, and buy a large cup of "get over yourself"... drink up...

    Monday, March 26, 2012 2:02 AM
  • Wow, you guys are really on top of your game right? You don't think he qoogled before he came to the forum? 

    Monday, March 26, 2012 2:06 AM
  • No evidence that he did. I have often responded to request by providing a link to google or codeproject or (horror of horrors) the help system, or somewhere else. I'd guess the proportion that used the link in the response to find a solution to be about 60%. So I'd guess a lot of people don't google or try the help system - especially those who request 'plz supply full code' (not that OP is one of those).


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Monday, March 26, 2012 3:52 PM
  • Nothing requires that you kiss up to anyone (except own inadequacies?). If it stinks and you can't stand the smell move on. :)

    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Monday, March 26, 2012 3:53 PM
  • For the record, I googled how to do this and got taken to this page. 
    Wednesday, April 04, 2012 1:37 PM
  • I just have to say this truth in this statement:

    "The only valid measurement of code quality: WTFs/minute."

    about made me wet my pants.

    Barrett Alexander

    Monday, May 21, 2012 2:23 PM
  • string yr;
    string 
    moday;
    string 
    hr;
    int 
    sec;
    string 
    hexsec;
    yr Hex((dtvar.Year - 2000)).Substring((Hex((dtvar.Year - 2000)).Length - 1));
    moday ("00" + Format(dtvar, "y")).Substring((("00" + Format(dtvar, "y")).Length - 3));
    hr ((char)((dtvar.Hour + 65)));
    sec ((dtvar.Minute * 60
                + dtvar.Second)
    ;
    hexsec ("00" + Hex(sec).ToString()).Substring((("00" + Hex(sec).ToString()).Length - 3));
    GenerateDocNumber (yr 
                + (moday + (
    "-" 
                
    + (hr + hexsec))));
    Tuesday, June 05, 2012 2:13 PM
  • The problem I find with rndNum = new Random() is that Random by default uses the system time as a seed. If you are trying to create many random numbers all at the same time on any modern processor you end up with all the results the same.

    MS provided a very handy "random" generator called the GUID. I like to use a GUID as a seed for each new random number. Then I can generate thousands of random numbers all at the same time without worrying if they are going to be the same.

    An example of this method is below.

    public static int RandNumber(int Low, int High)
    {
            Random rndNum = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber));
    
            int rnd = rndNum.Next(Low, High);
    
            return rnd;
    }


    • Edited by rsoult3 Friday, July 27, 2012 8:08 PM grammer
    Friday, July 27, 2012 8:07 PM
  • You can use the class RandomNumberGenerator in System.Security.Cryptography too.

    I kinda like the method provided by rsoult3, it's short (not many lines) :).

    Tuesday, July 31, 2012 7:58 PM
  • "experiment a little, don't you think" ... Listen here you pompous jerk, if he had the answer he would not have to beg for help on this page filled with know-it-alls. Believe me, it stinks having to kiss up to all the eggheads for answers. But hey- some of us have jobs that require a lot of time, and we don't have time to troll the knowledgebase for people who don't know what they are doing.... So go done to the corner market, and buy a large cup of "get over yourself"... drink up...

    He posted that 6 years ago... You sure know how to hold a grudge. 
    Tuesday, August 28, 2012 4:02 AM
  • wow, it's amazing how you can write that long message in the short time you have, i'm so impressed!
    Monday, September 24, 2012 4:10 PM
  • Um...You do realize you're yelling at a post that was made 6 years ago right?  Not to mention he was saying to search the MSDN documentation...not google.

    Just my two cents.

    Monday, September 24, 2012 7:49 PM
  • Here is a detailed article with code sample:

    How to work with Random Numbers in C#


    www.c-sharpcorner.com

    Monday, November 26, 2012 9:22 PM