Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc c# random generating same number [duplicate]

Tags:

c#

asp.net-mvc

how can i generate different number,it is generating the same number

Random rand = new Random(100000);
rand.Next();
like image 634
maztt Avatar asked Jan 20 '11 13:01

maztt


People also ask

What is MVC in .NET C#?

Model View Controller (MVC) MVC is a design pattern used to decouple user-interface (view), data (model), and application logic (controller). This pattern helps to achieve separation of concerns.

Is ASP.NET MVC still used?

ASP.NET MVC is no longer in active development. The last version update was in November 2018. Despite this, a lot of projects are using ASP.NET MVC for web solution development. As to JetBrains' research, 42% of software developers were using the framework in 2020.

What is difference between C# and ASP.NET MVC?

They are the same thing. C# is the language you have used to do your development, but ASP.NET MVC is the framework you used to do it.

Is ASP.NET MVC easy to learn?

It's really difficult to try and learn an entirely new language/framework under pressure. If you're required to deliver working software for your day job, trying to learn ASP.NET Core at the same time might be heaping too much pressure on yourself.


2 Answers

Just remove the seed number in the constructor. This seed is essentially a number from which the random number list is generated. If you specify a constant number, your random number list will always be the same.

Random rand = new Random();
rand.Next();
like image 106
Pierre-Alain Vigeant Avatar answered Sep 27 '22 21:09

Pierre-Alain Vigeant


Your specifying the same seed try this.

Random rand = new Random();
rand.Next();

This will use the default seed which is the time.

"Initializes a new instance of the Random class, using a time-dependent default seed value."

As per MSDN : http://msdn.microsoft.com/en-us/library/system.random.aspx

Re your comment above, how to generate a "random" number in a set range.

// Generate and display 5 random integers from 50 to 100.
Console.WriteLine("Five random integers between 50 and 100:");
Console.Write("{0,8:N0}", rand.Next(50, 101));

(Taken from MSDN link above) You can now generate whatever range you want.

like image 26
LiamB Avatar answered Sep 27 '22 22:09

LiamB