How to generate random number between 1 to 100 in c#. [Resolved]

Posted by Programmer123 under C# on 9/26/2013 | Points: 10 | Views : 17778 | Status : [Member] | Replies : 6
How to generate random number between 1 to 100 in c#.




Responses

Posted by: Xakzyy on: 9/26/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
The Random class is used to create random numbers. (Pseudo-random that is of course.)

Example:

Random rnd = new Random();

int month = rnd.Next(1, 13); // creates a number between 1 and 12
int dice = rnd.Next(1, 7); // creates a number between 1 and 6
int card = rnd.Next(52); // creates a number between 0 and 51


or for your use.


Random rnd = new Random();
int number = rnd.Next(100); // creates a number between 0 and 100[/code]

If you are going to create more than one random number, you should keep the Random instance and reuse it. If you create new instances too close in time, they will produce the same series of random numbers as the random generator is seeded from the system clock.

Hope it helps :)

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/26/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
You can use
Random.Next(int maxValue)


Return: A 32-bit signed integer greater than or equal to zero, and less than maxValue; that is, the range of return values ordinarily includes zero but not maxValue. However, if maxValue equals zero, maxValue is returned.

var r =newRandom();// print random integer >= 0 and  < 100

Console.WriteLine(r.Next(100));

For this case however you could use

Random.Next(int minValue, int maxValue), like this:// print random integer >= 1 and< 101


Console.WriteLine(r.Next(1,101);)// or perhaps (if you have this specific case)
Console.WriteLine(r.Next(100)+1);


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can use the below one:-

var random = new Random();
string ran = random.Next(1, 100000);


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
The Random class is used to create random numbers. (Pseudo-random that is of course.)

Example:

Random rnd = new Random();
int month = rnd.Next(1, 13); // creates a number between 1 and 12
int dice = rnd.Next(1, 7); // creates a number between 1 and 6
int card = rnd.Next(52); // creates a number between 0 and 51


If you are going to create more than one random number, you should keep the Random instance and reuse it. If you create new instances too close in time, they will produce the same series of random numbers as the random generator is seeded from the system clock.


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You can see the below links:-

http://www.dotnetperls.com/random
http://social.msdn.microsoft.com/Forums/en-US/55fb3116-c978-4ac8-9381-a2605e16e256/how-do-you-create-a-random-number-in-c
http://forums.asp.net/t/1262293.aspx
http://www.c-sharpcorner.com/UploadFile/mahesh/RandomNumber11232005010428AM/RandomNumber.aspx

Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/26/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
IEnumerable<int> numbers =Enumerable.Repeat(1,N).Select(_ => random.Next(100));

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Programmer123, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response