6

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

Random rand = new Random(100000);
rand.Next();
tereško
  • 58,060
  • 25
  • 98
  • 150
maztt
  • 12,278
  • 21
  • 78
  • 153

5 Answers5

13

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();
Pierre-Alain Vigeant
  • 22,635
  • 8
  • 65
  • 101
  • will it restrict the number of characters generated or it can generate 10000 or 1000000000 – maztt Jan 20 '11 at 13:26
  • I don't think that there is a limit to the amount of number generated. It is also unrelated to a numbers of character since the `.Next()` method returns a 32-bit signed integer greater than or equal to zero and less than MaxValue. – Pierre-Alain Vigeant Jan 20 '11 at 13:29
8

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.

LiamB
  • 18,243
  • 19
  • 75
  • 116
  • There's a potential problem with using the default time-based seed. For a multi-threaded app running on a multi-core processor, it becomes relatively likely for two different threads to generate the exact same random value (since it becomes possible for them to grab the exact same system time and thus produce the same value). I'd use a hash of a Guid.NewGuid() as the seed instead. – MusiGenesis Jan 20 '11 at 13:37
  • If you were devolping such an application then correct, however the use of the Random() class shouldnt be used for anything other than making things appear random. – LiamB Jan 20 '11 at 13:41
  • The question is tagged with "asp.net" which is exactly the kind of multi-threaded environment that can produce these kinds of issues. Two users hitting the same code at the same time might see the same "random" values. – LukeH Jan 20 '11 at 13:57
  • +1 for mentioning rand.Next(Min, max);. Was going to put an answer that added that. Emphasize the 101 to generate 50 to 100. – WernerCD Jan 20 '11 at 15:14
  • @LukeH - I dont agree disagree. Its just not what he asked :) – LiamB Jan 20 '11 at 16:29
4

Not sure exactly what you are after!

Random rand = new Random(Environment.TickCount);
rand.Next();
WraithNath
  • 17,658
  • 10
  • 55
  • 82
  • +1 while the tickcount isn't a great seed it's atleast dynamic and for general purposes probably *good enough*. – Chris Marisic Jan 20 '11 at 13:24
  • In the future, it might be better not to attempt to answer a question when, as you say, you are not sure exactly what they are after. – DOK Jan 20 '11 at 13:24
  • ok thanks, looks like I assumed the same as everyone else anyway :) – WraithNath Jan 20 '11 at 13:24
  • 2
    The default `Random` constructor uses `Environment.TickCount` anyway. There's no need to specify it explicitly. – LukeH Jan 20 '11 at 13:29
  • @LukeH - thanks, Just thought it was a fair way to specify a seed, didnt know it actually used it! – WraithNath Jan 20 '11 at 13:31
3
Random rand = new Random();
rand.Next(0,1000); // minimum = 0, maximum = 999
Lawyerson
  • 919
  • 1
  • 7
  • 25
danbord
  • 3,605
  • 3
  • 33
  • 49
  • 7
    Worth saying that the minimum is inclusive, the maximum is exclusive. The above will generate a number in the (inclusive) range 0-999. – Iain Galloway Jan 20 '11 at 13:32
3
Random rand=new Random(DateTime.Now.Millisecond);
rand.Next();

This always works for me.

Gustavo Puma
  • 993
  • 12
  • 27
  • 1
    Why not just `new Random()`, since it uses `Environment.TickCount` as the default seed? – LukeH Jan 20 '11 at 13:31
  • @LukeH: do you have any documentation on that? All I've ever seen documented is that it's seeded with a time-based value, which doesn't necessarily mean it's using `Environment.TickCount`. – MusiGenesis Jan 20 '11 at 13:40
  • @MusiGenesis: I think it's an implementation detail rather than a specified guarantee, but it's easy enough to confirm for yourself using ILDASM or Reflector. Or doing something like `var r1 = new Random(); var r2 = new Random(Environment.TickCount); while (true) Console.WriteLine(r1.Next() == r2.Next());` should display "true" continuously unless you're *really* unlucky and the two constructors run on either side of a tick. – LukeH Jan 20 '11 at 13:53