Skip to main content

My answer from here:

Just reiterating the right solution:

namespace mySpace { public static class Util { private static Random rnd = new Random(); public static int GetRandom() { return rnd.Next(); } } } 

So you can call:

var i = Util.GetRandom(); 

all throughout.

If you strictly need a true stateless static method to generate random numbers, you can rely on a Guid.

public static class Util { public static int GetRandom() { return Guid.NewGuid().GetHashCode(); } } 

It's going to be a wee bit slower, but can be much more random than Random.Next, at least from my experience.

But not:

new Random(Guid.NewGuid().GetHashCode()).Next(); 

The unnecessary object creation is going to make it slower especially under a loop.

And never:

new Random().Next(); 

Not only it's slower (inside a loop), its randomness is... well not really good according to me..

My answer from here:

Just reiterating the right solution:

namespace mySpace { public static class Util { private static rnd = new Random(); public static int GetRandom() { return rnd.Next(); } } } 

So you can call:

var i = Util.GetRandom(); 

all throughout.

If you strictly need a true stateless static method to generate random numbers, you can rely on a Guid.

public static class Util { public static int GetRandom() { return Guid.NewGuid().GetHashCode(); } } 

It's going to be a wee bit slower, but can be much more random than Random.Next, at least from my experience.

But not:

new Random(Guid.NewGuid().GetHashCode()).Next(); 

The unnecessary object creation is going to make it slower especially under a loop.

And never:

new Random().Next(); 

Not only it's slower (inside a loop), its randomness is... well not really good according to me..

My answer from here:

Just reiterating the right solution:

namespace mySpace { public static class Util { private static Random rnd = new Random(); public static int GetRandom() { return rnd.Next(); } } } 

So you can call:

var i = Util.GetRandom(); 

all throughout.

If you strictly need a true stateless static method to generate random numbers, you can rely on a Guid.

public static class Util { public static int GetRandom() { return Guid.NewGuid().GetHashCode(); } } 

It's going to be a wee bit slower, but can be much more random than Random.Next, at least from my experience.

But not:

new Random(Guid.NewGuid().GetHashCode()).Next(); 

The unnecessary object creation is going to make it slower especially under a loop.

And never:

new Random().Next(); 

Not only it's slower (inside a loop), its randomness is... well not really good according to me..

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

My answer from herehere:

Just reiterating the right solution:

namespace mySpace { public static class Util { private static rnd = new Random(); public static int GetRandom() { return rnd.Next(); } } } 

So you can call:

var i = Util.GetRandom(); 

all throughout.

If you strictly need a true stateless static method to generate random numbers, you can rely on a Guid.

public static class Util { public static int GetRandom() { return Guid.NewGuid().GetHashCode(); } } 

It's going to be a wee bit slower, but can be much more random than Random.Next, at least from my experience.

But not:

new Random(Guid.NewGuid().GetHashCode()).Next(); 

The unnecessary object creation is going to make it slower especially under a loop.

And never:

new Random().Next(); 

Not only it's slower (inside a loop), its randomness is... well not really good according to me..

My answer from here:

Just reiterating the right solution:

namespace mySpace { public static class Util { private static rnd = new Random(); public static int GetRandom() { return rnd.Next(); } } } 

So you can call:

var i = Util.GetRandom(); 

all throughout.

If you strictly need a true stateless static method to generate random numbers, you can rely on a Guid.

public static class Util { public static int GetRandom() { return Guid.NewGuid().GetHashCode(); } } 

It's going to be a wee bit slower, but can be much more random than Random.Next, at least from my experience.

But not:

new Random(Guid.NewGuid().GetHashCode()).Next(); 

The unnecessary object creation is going to make it slower especially under a loop.

And never:

new Random().Next(); 

Not only it's slower (inside a loop), its randomness is... well not really good according to me..

My answer from here:

Just reiterating the right solution:

namespace mySpace { public static class Util { private static rnd = new Random(); public static int GetRandom() { return rnd.Next(); } } } 

So you can call:

var i = Util.GetRandom(); 

all throughout.

If you strictly need a true stateless static method to generate random numbers, you can rely on a Guid.

public static class Util { public static int GetRandom() { return Guid.NewGuid().GetHashCode(); } } 

It's going to be a wee bit slower, but can be much more random than Random.Next, at least from my experience.

But not:

new Random(Guid.NewGuid().GetHashCode()).Next(); 

The unnecessary object creation is going to make it slower especially under a loop.

And never:

new Random().Next(); 

Not only it's slower (inside a loop), its randomness is... well not really good according to me..

fixed it's and its
Source Link
frenchie
  • 52.3k
  • 117
  • 321
  • 532

My answer from here:

Just reiterating the right solution:

namespace mySpace { public static class Util { private static rnd = new Random(); public static int GetRandom() { return rnd.Next(); } } } 

So you can call:

var i = Util.GetRandom(); 

all throughout.

If you strictly need a true stateless static method to generate random numbers, you can rely on a Guid.

public static class Util { public static int GetRandom() { return Guid.NewGuid().GetHashCode(); } } 

It's going to be a wee bit slower, but can be much more random than Random.Next, at least from my experience.

But not:

new Random(Guid.NewGuid().GetHashCode()).Next(); 

The unnecessary object creation is going to make it slower especially under a loop.

And never:

new Random().Next(); 

Not only itsit's slower (inside a loop), it'sits randomness is... well not really good according to me..

My answer from here:

Just reiterating the right solution:

namespace mySpace { public static class Util { private static rnd = new Random(); public static int GetRandom() { return rnd.Next(); } } } 

So you can call:

var i = Util.GetRandom(); 

all throughout.

If you strictly need a true stateless static method to generate random numbers, you can rely on a Guid.

public static class Util { public static int GetRandom() { return Guid.NewGuid().GetHashCode(); } } 

It's going to be a wee bit slower, but can be much more random than Random.Next, at least from my experience.

But not:

new Random(Guid.NewGuid().GetHashCode()).Next(); 

The unnecessary object creation is going to make it slower especially under a loop.

And never:

new Random().Next(); 

Not only its slower (inside a loop), it's randomness is... well not really good according to me..

My answer from here:

Just reiterating the right solution:

namespace mySpace { public static class Util { private static rnd = new Random(); public static int GetRandom() { return rnd.Next(); } } } 

So you can call:

var i = Util.GetRandom(); 

all throughout.

If you strictly need a true stateless static method to generate random numbers, you can rely on a Guid.

public static class Util { public static int GetRandom() { return Guid.NewGuid().GetHashCode(); } } 

It's going to be a wee bit slower, but can be much more random than Random.Next, at least from my experience.

But not:

new Random(Guid.NewGuid().GetHashCode()).Next(); 

The unnecessary object creation is going to make it slower especially under a loop.

And never:

new Random().Next(); 

Not only it's slower (inside a loop), its randomness is... well not really good according to me..

Source Link
nawfal
  • 73.8k
  • 59
  • 342
  • 379
Loading