Jump to content




Recent Status Updates

  • Photo
      15 Nov
    duzamucha

    Hi, I am final year Interior Design Student from University of Huddersfield. I am currently working on my final major project which is going to be linked to coding. I was hoping that you could help me with my research. I have prepared a short survey, it would be a massive help if you could fill it in for me. It takes less than 2 minutes to complete, I promise. Here is the link: https://www.surveymonkey.com/s/73XLJKK Thank you so much in advance!

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

C# Tutorial: Generating Random Numbers.

random number

  • Please log in to reply
23 replies to this topic

#1 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 21 January 2010 - 02:24 PM

Hello, Today I'm going to show how to generate random numbers in C#.

OK so there are a couple different ways to generate random numbers.

The first way we can do this is to add this code:
Random randnum = new Random();
            int num = randnum.Next();

So what that did was make a new random number named randnum then assigned a new variable named num equal to the random number.(we do this so we can print the number if we want.)

Now what you may have noticed is that will make a random number but you probably want your number to have a limit to do this we change the code to:
Random random = new Random();
 int num = random.Next(100);

So this will generate a positive number up to 100 including 0, which is good but in some case you won't want 0 to be picked like if you where picking a raffle winner or something to that effect.

So the last and most used way is to change the code to:
Random random = new Random();
            int num = random.Next(1, 100);
So now it generates a number between 1 and 100.

OK so now that your know how to generate a random number lets make a dice roller to demonstrate how to use it.

So first make a new project and add a label and a button to your form.
Now double click the button and add this code:
Random random = new Random();
            int num = random.Next(1, 6);
            label1.Text = Convert.ToString(num);
then try it out.

So the code label1.Text = Convert.ToString(num); takes are variable that's equal to the random number and converts it to a string and prints it out in the label.

So there you go, now you know how to generate and print random numbers.
Hope you enjoyed this tutorial, if you have any questions just ask.:)
  • 1
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#2 gokuajmes

gokuajmes

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 483 posts

Posted 22 January 2010 - 12:58 AM

there is something interesting to see.
  • 0

#3 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 22 January 2010 - 06:35 AM

Thanks.:)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#4 Bartimäus

Bartimäus

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 128 posts

Posted 23 January 2010 - 05:03 PM

Good tut!
  • 0
[SIGPIC][/SIGPIC]

#5 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3,033 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 23 January 2010 - 05:37 PM

 int num = random.Next(1, 100);

That is a lot like how PHP does it. Very interesting! It's nice that you can easily choose a range for the random numbers.
  • 0

#6 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 23 January 2010 - 09:39 PM

That is a lot like how PHP does it.

really, cool didn't know that.BTW thanks for the +rep.:)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#7 jendomatic

jendomatic

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 03 April 2010 - 02:09 AM

How do you generate two random numbers at the same time and then multiply, divide, add or subtract those numbers together?
  • 0

#8 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 03 April 2010 - 04:15 AM

Just make a new random number(make sure not to name it random like are first one) like this:
Random [COLOR=Red][B]randomGen1[/B][/COLOR] = new Random();

int [B]num1[/B] = random.Next(1, 6);



Random [COLOR=Red][B]randomGen2[/B][/COLOR] = new Random();

int [B]num2[/B] = random.Next(1, 6);









int total = num1 + num2;

Simple as that, as long as you give the random generator another name you can make as many random numbers as you want. :)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#9 24kmax10

24kmax10

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 10 April 2010 - 07:57 AM

thx a bunch.but i still gat a prob i wana generate only two characters a X or a O,one at a time.ave tried this out its jst fyn but i think ineed yo help again
  • 0

#10 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 10 April 2010 - 09:11 AM

Ok well theres two ways I would do it, 1. generate a random number(1 or 2) then run a if loop and if the number is 1 then do what you would in the case of X then if its 2 do what you would do if it was a O. 2. Whic I suggest more is to store the letters you want into an array then generate a random array value. If you don't know how to use arrays see my tutorial here: C# Tutorial Arrarys

  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#11 xxxxjayxxx

xxxxjayxxx

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 123 posts

Posted 12 May 2010 - 05:22 AM

hmm ... i want to create a guessing game with 1 textbox , 1 label and 2 button (Enter and New Game) . Basically this game is to generate random number in the label and ask us to enter our own number in the textbox . The problem i face is when I click enter the random number will keep changing . How can i store the random number to prevent it from changing ?
  • 0

#12 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 12 May 2010 - 06:35 AM

Well it would be easier if I could see your code, but since you didn't post it....... It's because every time you click the Enter button or whatever your generating the random number. try just generating the random number when you start the game and on when you press the new game button. If you don't understand post your code, and I'll help you. ;)
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.





Powered by binpress