Reputation: 2055
A lot of times when I draw numbers I get 0 and all the time I can see similar draw sequences. Do you know why?
For example: losowanie(3) -> result 0 some times 1?
Code:
public int losowanie(int w)
{
Random rand = new Random(Guid.NewGuid().GetHashCode());
int s = w - 1;
return rand.Next(0, s);
}
Code:
Upvotes: 1
Views: 92
Reputation: 42444
Instantiate your Random class once and reuse that instance. For example by having a private static field in the class that utilizes the Random sequence. As an aside: I doubt a default seed with Guid.NewGuid() will give better results as to the the parameterless contructor of Random().
public class whatever
{
static Random rand = new Random(Guid.NewGuid().GetHashCode());
// or use new Random();
public int losowanie(int w)
{
int s = w - 1;
return rand.Next(0, s);
}
}
Upvotes: 1
Reputation: 17353
The behavior you're seeing is correct - losowanie(3)
calls rand.Next(0, 2)
so only two values can be returned - either 0
or 1
.
You may misunderstand the meaning of Random.Next(minValue, maxValue)
parameters - check MSDN for details.
Upvotes: 1