Jump to content

C# Tutorial: Generating Random Numbers.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
21 replies to this topic

#1
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
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.:)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
there is something interesting to see.

#3
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Thanks.:)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#4
Bartimäus

Bartimäus

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Good tut!
[SIGPIC][/SIGPIC]

#5
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
 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.

#6
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts

Quote

That is a lot like how PHP does it.
really, cool didn't know that.BTW thanks for the +rep.:)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#7
jendomatic

jendomatic

    Newbie

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

#8
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
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. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#9
24kmax10

24kmax10

    Newbie

  • Members
  • Pip
  • 9 posts
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

#10
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
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

A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#11
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
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 ?

#12
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
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. ;)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.