Pages

Monday, April 4, 2011

Random Number Generator C#

Ok so it took about 5 minutes to make the generator so will make the tutorial now. Download my project  from HERE, other wise follow the instructions.


First things first you will need Visual Studio if you already haven't got it. I found an express edition of you want to trial it from HERE choose your language and download (I used C# for the language, you can choose anyone you like but syntax may be different).


Once you have everything installed, open up Visual Studio, and start a new project by going upto file / new project .. you will then get a screen like this.

















Select a "Windows Forms Application" name your project and click OK.


Go ahead and use the toolbox to put a Text box, button, and label (optional) onto the form. Add a little colour if you like, *I know mine is an eyesore* but I don't care :P. It should now look something like this.














Double click the button, this will take you to the "Button click handler" where you code what you want to happen when the button is clicked.












Inside the button1_Click's curly brackets, (these things {  }), start to type "Random" visual studio will drop down a box of what it thinks you are wanting to type, this will automatically complete what you want to type. I have written out all the code below with comments to read of what is happening. the "//" is commented line which isn't included in the code.














I will go through it line by line, to people unaware of the basics of coding you may struggle to wrap your head around what is being done, I am not very good at explaining stuff :P so you might have to google search the blue prints of coding. 


Random RandNum = new Random(); VS has built in "static classes" which are very helpful in this situation, here I am creating a new instance of this class to be used in the project.


String NewNumber; Creating a string variable to put the random number we are going to make inside it.


NewNumber = Convert.ToString(RandNum.Next(1000) + 1); This is a sloppy way of doing it but it works and is easy to follow. This will create a new random number no bigger than "1000" as we have told it. The +1 is because this is 0 based which means if we were to count to 5 it would be 0,1,2,3,4, the +1 allows it to be the real number. We have to "Convert.ToString" so it can be put into the String variable we created, at the moment it is an Integer and the 2 are not able to be compared unless converted.


TextBox.Text = NewNumber; Make the text inside the textbox = the random number.


You can now proceed to run your project and see what you get.


Here is the final outcome: (Changed colour for non eye hurtness :))












Done! This was a bit of a rush job so any questions are more than welcome in the comments below.

15 comments:

Wumbo said...

I will most definitely use this just in case I want to have a drawing for my followers. Thanks for sharing!

DYLON said...

This is awesome man, cool stuff!

izzfoshizzz said...

too complex for me. think theres a class to do stuff like this at my school

Stef Pelgrims said...

woah cool; looks like java

Anonymous said...

nice. thanks for your time in doing this.

toadeye said...

My friend has recently started learning programming. I will direct his attention to this.

M Osborne said...

Wow great tutorial

doomsday said...

I want to code too, but I'm lazy to learn :D

Unknown said...

it's a pseudo random number generator at best

Manhood88 said...

pretty swift. Thanks for the guild. Will defiantly be saving it for some other time.

Patti D. said...

awesome application!!

frameaj1 said...

@mac-and-me: yeah it was all that was needed :)

Lemmiwinks said...

i like your blog. keep up the good work

cool_cicler said...

More tutorials like this are needed, keep on posting please.

tissue rejection said...

Sweet program! I like.

Post a Comment