Random.Next returns always the same values
The problem is that you are creating instances of the Random
class too close in time.
When you create a Random
object, it's seeded with a value from the system clock. If you create Random
instances too close in time, they will all be seeded with the same random sequence.
Create a single Random
object and pass its reference to the constructor when you create instances of the "a" class, instead of creating one Random
object for each "a" instance.
Random.Next returns always the same values
You have three problems with your code.
- You should externalize your random variable.
- You have a problem with truncation error.
- The range between min and max is way to large.
The first problem is because you may not have enough time to advance the seed when reinitializing your random variable. The second error comes from truncating your (what would b very large) numbers down to ints. Finally, your biggest problem is your range between your min and your max. Consider finding the range between min and max (as defined in your code) with inputs 1->20:
length max-min
1 8
2 89
3 899
4 8999
5 89999
6 899999
7 8999999
8 89999999
9 899999999
10 8,999,999,999
11 89999999999
12 899999999999
13 8999999999999
14 89999999999999
15 899999999999999
16 9E+15
17 9E+16
18 9E+17
19 9E+18
And keep in mind that the maximum integer is 2,147,483,647, which is passed on any number greater than 9.
Why does Random.Next() always return the same number
You're always seeding a new instance with the same seed, and then grabbing the first max. By using a Seed, you're guaranteeing the same results.
If you want to have a static, random number generation that does different results, you should rework this a bit. However, since Random is not threadsafe, it requires some synchronization when used statically. Something like:
private static Random random;
private static object syncObj = new object();
private static void InitRandomNumber(int seed)
{
random = new Random(seed);
}
private static int GenerateRandomNumber(int max)
{
lock(syncObj)
{
if (random == null)
random = new Random(); // Or exception...
return random.Next(max);
}
}
Random.Next() always returns same values in for loop
rand.Next(0, 3) / 100
is always 0. You get an integer thats either 0,1 or 2. Then divide by 100, this gets rounded down to 0.
You want
rand.NextDouble()
and the adjust the range of the result to match what yout need. (It returns a number from 0.0 to < 1.0)
Random.Next() gives me the same value
The Next method returns a pseudo-random number between int.MinValue and 0. It does not guarantee the numbers will be unique. What do you think happens if you call Random.Next(0,10) 11 times?
To prevent duplicates you'll need to keep track of which Ids have been issued.
Alternatively, is there any need for the Ids to be in a random order? Could you just use an incrementing int to generate Ids?
C# random function returning the same value
When you create a Random
instance it's seeded with the current time. So if you create several of them at the same time they'll generate the same random number sequence. You need to create a single instance of Random
and use that.
System.Random().Next() returning the same result
It's best explained by the manual of System.Random()'s default constructor;
The default seed value is derived from the system clock and has finite
resolution. As a result, different Random objects that are created in
close succession by a call to the default constructor will have
identical default seed values and, therefore, will produce identical
sets of random numbers.
Random.Next() sometimes returns same number in separate threads
Random is not thread-safe - you shouldn't be using the same instance from multiple threads. It can get much worse than just returning the same data - by using it from multiple threads, you can get it "stuck" in a state where it will always return 0, IIRC.
Obviously you don't just want to create a new instance for each thread at roughly the same time, as they'll end up with the same seeds...
I have an article which goes into the details of this and provides an implementation which lazily instantiates one instance of Random
per thread using an incrementing seed.
System.Random keeps on returning the same value
The random number generator in .NET is not thread safe. Other developers have noticed the same behaviour, and one solution is as follows (from http://blogs.msdn.com/brada/archive/2003/08/14/50226.aspx):
class ThreadSafeRandom
{
private static Random random = new Random();
public static int Next()
{
lock (random)
{
return random.Next();
}
}
}
Related Topics
How to Post Data to Specific Url Using Webclient in C#
Data Binding to Selecteditem in a Wpf Treeview
C# Catch a Stack Overflow Exception
Multiline String Literal in C#
Execute Powershell Script from C# With Commandline Arguments
Factory Method With Di and Ioc
What Is App.Config in C#.Net - How to Use It
Use Linq to Xml With Xml Namespaces
How to Get the Ip Address of the Server on Which My C# Application Is Running On
.Net Global Exception Handler in Console Application
Random.Next Returns Always the Same Values
Entity Framework - Include Multiple Levels of Properties
Understanding Covariant and Contravariant Interfaces in C#
Best Practice For Forcing Garbage Collection in C#
How to Load a .Net Assembly At Runtime and Instantiate a Type Knowing Only the Name