I'm trying to get a voice line/Sound to play on form show however I only can play two different sounds and Id like to be able to have a list that gets played at random every time the form is shown, Anyone know the best way to go about this? here's the code I have right now:
if (Properties.Settings.Default.UI > 0)
{
var random = new Random();
SoundPlayer audio = new SoundPlayer(_2B.Properties.Resources.russian);
audio.Play();
}
else
{
SoundPlayer audio = new SoundPlayer(_2B.Properties.Resources.freedom);
audio.Play();
}
I tried adding a var for the random pick but it didnt work, I was assuming I could possibly do the same thing as this:
if (settings.version < newversionparsed)
{
bunifuCircleProgressbar1.Value +=1;
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var stringChars = new char[8];
var random = new Random();
for (int i = 0; i < stringChars.Length; i++)
{
stringChars[i] = chars[random.Next(chars.Length)];
}
var finalString = new String(stringChars);
But im not too sure that it's working like I think, Nor do I know if this is possible.
Keep your Random so you would get a reasonable result
According to MSDN
On the .NET Framework, initializing two random number generators in a tight loop or in rapid succession creates two random number generators that can produce identical sequences of random numbers. In most cases, this is not the developer's intent and can lead to performance issues, because instantiating and initializing a random number generator is a relatively expensive process.
Both to improve performance and to avoid inadvertently creating separate random number generators that generate identical numeric sequences, we recommend that you create one Random object to generate many random numbers over time, instead of creating new Random objects to generate one random number.
So create one and use it multiple times is better.
Note: Random is not thread safe, but MSDN has an example for thread safe version.
Following is the example.
public static void Main()
{
Console.WriteLine("Hello World");
int len = 10;
int seed = 7;
string newEverytime = "";
for( int i =0 ; i < len; i++){
newEverytime+= new Random().Next( seed ).ToString();
}
Console.WriteLine( newEverytime ); // print something fixed
string get10Next="";
Random r = new Random();
for( int i =0 ; i < len; i++){
get10Next+=r.Next(seed);
}
Console.WriteLine( get10Next ); // print random string
}
For your program, you need to new a Random as a class member.
class Foo{
Random rnd = new Random();
void PlaySound(){
int val = rnd.Next( 1 ); // use a seed you like
// implement your play sound
}
}
Create an instance of Random somewhere, ideally static so you can easily reuse the same instance:
static Random random = new Random();
Create an array of all the sounds you want available to you:
string[] Stream = new Stream[] {
_2B.Properties.Resources.russian,
_2B.Properties.Resources.freedom
// Add as many sounds as you want
};
Then when you want to get a sound to play:
string sound = sounds[random.Next(sounds.Length)];
SoundPlayer audio = new SoundPlayer(sound);
How do I generate a random integer in C#?
The Random class is used to create random numbers. (Pseudo-random that is of course.).
Example:
Random rnd = new Random();
int month = rnd.Next(1, 13); // creates a number between 1 and 12
int dice = rnd.Next(1, 7); // creates a number between 1 and 6
int card = rnd.Next(52); // creates a number between 0 and 51
If you are going to create more than one random number, you should keep the Random instance and reuse it. If you create new instances too close in time, they will produce the same series of random numbers as the random generator is seeded from the system clock.
The question looks very simple but the answer is bit complicated. If you see almost everyone has suggested to use the Random class and some have suggested to use the RNG crypto class. But then when to choose what.
For that we need to first understand the term RANDOMNESS and the philosophy behind it.
I would encourage you to watch this video that I made which goes in depth in the philosophy of RANDOMNESS using C# https://www.youtube.com/watch?v=tCYxc-2-3fY
First thing let us understand the philosophy of RANDOMNESS. When we tell a person to choose between RED, GREEN and YELLOW what happens internally. What makes a person choose RED or YELLOW or GREEN?
Some initial thought goes into the persons mind which decides his choice, it can be favorite color , lucky color and so on. In other words some initial trigger which we term in RANDOM as SEED.This SEED is the beginning point, the trigger which instigates him to select the RANDOM value.
Now if a SEED is easy to guess then those kind of random numbers are termed as PSEUDO and when a seed is difficult to guess those random numbers are termed SECURED random numbers.
For example a person chooses is color depending on weather and sound combination then it would be difficult to guess the initial seed.
Now let me make an important statement:-
*“Random” class generates only PSEUDO random number and to generate SECURE random number we need to use “RNGCryptoServiceProvider” class.
Random class takes seed values from your CPU clock which is very much predictable. So in other words RANDOM class of C# generates pseudo random numbers , below is the code for the same.
Random random = new Random();
int randomNumber = random.Next();
While the RNGCryptoServiceProvider class uses OS entropy to generate seeds. OS entropy is a random value which is generated using sound, mouse click, and keyboard timings, thermal temp etc. Below goes the code for the same.
using (RNGCryptoServiceProvider rg = new RNGCryptoServiceProvider())
{
byte[] rno = new byte[5];
rg.GetBytes(rno);
int randomvalue = BitConverter.ToInt32(rno, 0);
}
To understand OS entropy see this video of mine starting at 14:30 https://www.youtube.com/watch?v=tCYxc-2-3fY where the logic of OS entropy is explained. So putting in simple words RNG Crypto generates SECURE random numbers.
Every time you do new Random() it is initialized. This means that in a tight loop you get the same value lots of times. You should keep a single Random instance and keep using Next on the same instance.
//Function to get random number
private static readonly Random getrandom = new Random();
public static int GetRandomNumber(int min, int max)
{
lock(getrandom) // synchronize
{
return getrandom.Next(min, max);
}
}
Beware that new Random() is seeded on current timestamp.
If you want to generate just one number you can use:
new Random().Next( int.MinValue, int.MaxValue )
For more information, look at the Random class, though please note:
However, because the clock has finite resolution, using the parameterless constructor to create different Random objects in close succession creates random number generators that produce identical sequences of random numbers
So do not use this code to generate a series of random number.
Random r = new Random();
int n = r.Next();
I wanted to add a cryptographically secure version:
RNGCryptoServiceProvider Class (MSDN or dotnetperls)
It implements IDisposable.
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
byte[] randomNumber = new byte[4];//4 for int32
rng.GetBytes(randomNumber);
int value = BitConverter.ToInt32(randomNumber, 0);
}
create a Random object
Random rand = new Random();
and use it
int randomNumber = rand.Next(min, max);
you don't have to initialize new Random() every time you need a random number, initiate one Random then use it as many times as you need inside a loop or whatever
You could use Jon Skeet's StaticRandom method inside the MiscUtil class library that he built for a pseudo-random number.
using MiscUtil;
...
for (int i = 0; i < 100;
Console.WriteLine(StaticRandom.Next());
I've tried all of these solutions excluding the COBOL answer... lol
None of these solutions were good enough. I needed randoms in a fast for int loop and I was getting tons of duplicate values even in very wide ranges. After settling for kind of random results far too long I decided to finally tackle this problem once and for all.
It's all about the seed.
I create a random integer by parsing out the non-digits from Guid, then I use that to instantiate my Random class.
public int GenerateRandom(int min, int max)
{
var seed = Convert.ToInt32(Regex.Match(Guid.NewGuid().ToString(), #"\d+").Value);
return new Random(seed).Next(min, max);
}
Update: Seeding isn't necessary if you instantiate the Random class once. So it'd be best to create a static class and call a method off that.
public static class IntUtil
{
private static Random random;
private static void Init()
{
if (random == null) random = new Random();
}
public static int Random(int min, int max)
{
Init();
return random.Next(min, max);
}
}
Then you can use the static class like so..
for(var i = 0; i < 1000; i++)
{
int randomNumber = IntUtil.Random(1,100);
Console.WriteLine(randomNumber);
}
I admit I like this approach better.
The numbers generated by the inbuilt Random class (System.Random) generates pseudo random numbers.
If you want true random numbers, the closest we can get is "secure Pseudo Random Generator" which can be generated by using the Cryptographic classes in C# such as RNGCryptoServiceProvider.
Even so, if you still need true random numbers you will need to use an external source such as devices accounting for radioactive decay as a seed for an random number generator. Since, by definition, any number generated by purely algorithmic means cannot be truly random.
Modified answer from here.
If you have access to an Intel Secure Key compatible CPU, you can generate real random numbers and strings using these libraries: https://github.com/JebteK/RdRand and https://www.rdrand.com/
Just download the latest version from here, include Jebtek.RdRand and add a using statement for it. Then, all you need to do is this:
// Check to see if this is a compatible CPU
bool isAvailable = RdRandom.GeneratorAvailable();
// Generate 10 random characters
string key = RdRandom.GenerateKey(10);
// Generate 64 random characters, useful for API keys
string apiKey = RdRandom.GenerateAPIKey();
// Generate an array of 10 random bytes
byte[] b = RdRandom.GenerateBytes(10);
// Generate a random unsigned int
uint i = RdRandom.GenerateUnsignedInt();
If you don't have a compatible CPU to execute the code on, just use the RESTful services at rdrand.com. With the RdRandom wrapper library included in your project, you would just need to do this (you get 1000 free calls when you signup):
string ret = Randomizer.GenerateKey(<length>, "<key>");
uint ret = Randomizer.GenerateUInt("<key>");
byte[] ret = Randomizer.GenerateBytes(<length>, "<key>");
As described in other answers, a good secured approach would be to use a secure cryptographic generator. All examples here show the usage of RNGCryptoServiceProvider which is writing a long code compared to the solution I suggest.
Use RandomNumberGenerator which is written on top of cryptography API’s. It is as secure as RNGCryptoServiceProvider and same randomness.
// Gives a random number for the integer range.
// You can simply update the parameters as your needs.
RandomNumberGenerator.GetInt32(int.MinValue, int.MaxValue);
Just as a note for future reference.
If you're using .NET Core, multiple Random instances aren't as dangerous as before. I'm aware that this question is from 2010, but since this question is old but has some attraction, I think it's a good thing to document the change.
You may refer to this question I made a while back:
Did Microsoft change Random default seed?
Basically, they have changed the default seed from Environment.TickCount to Guid.NewGuid().GetHashCode(), so if you create 2 instances of Random it shouldn't display the same numbers (1:4 billion).
You can see the file diffs from .NET Framework/.NET Core (2.0.0+) here: https://github.com/dotnet/coreclr/pull/2192/commits/9f6a0b675e5ac0065a268554de49162c539ff66d
It isn't as safe as RNGCryptoServiceProvider, but at least it won't give you weird results.
By #Enigmativity:
This is now out-of-date. There was a considerable backlash against using Guids. The code is now Interop.GetRandomBytes((byte*)&result, sizeof(int));
This is the class I use. Works like RandomNumber.GenerateRandom(1, 666)
internal static class RandomNumber
{
private static Random r = new Random();
private static object l = new object();
private static Random globalRandom = new Random();
[ThreadStatic]
private static Random localRandom;
public static int GenerateNewRandom(int min, int max)
{
return new Random().Next(min, max);
}
public static int GenerateLockedRandom(int min, int max)
{
int result;
lock (RandomNumber.l)
{
result = RandomNumber.r.Next(min, max);
}
return result;
}
public static int GenerateRandom(int min, int max)
{
Random random = RandomNumber.localRandom;
if (random == null)
{
int seed;
lock (RandomNumber.globalRandom)
{
seed = RandomNumber.globalRandom.Next();
}
random = (RandomNumber.localRandom = new Random(seed));
}
return random.Next(min, max);
}
}
While this is okay:
Random random = new Random();
int randomNumber = random.Next()
You'd want to control the limit (min and max mumbers) most of the time. So you need to specify where the random number starts and ends.
The Next() method accepts two parameters, min and max.
So if i want my random number to be between say 5 and 15, I'd just do
int randomNumber = random.Next(5, 16)
I wanted to demonstrate what happens when a new random generator is used every time. Suppose you have two methods or two classes each requiring a random number. And naively you code them like:
public class A
{
public A()
{
var rnd=new Random();
ID=rnd.Next();
}
public int ID { get; private set; }
}
public class B
{
public B()
{
var rnd=new Random();
ID=rnd.Next();
}
public int ID { get; private set; }
}
Do you think you will get two different IDs? NOPE
class Program
{
static void Main(string[] args)
{
A a=new A();
B b=new B();
int ida=a.ID, idb=b.ID;
// ida = 1452879101
// idb = 1452879101
}
}
The solution is to always use a single static random generator. Like this:
public static class Utils
{
public static readonly Random random=new Random();
}
public class A
{
public A()
{
ID=Utils.random.Next();
}
public int ID { get; private set; }
}
public class B
{
public B()
{
ID=Utils.random.Next();
}
public int ID { get; private set; }
}
Numbers calculated by a computer through a deterministic process, cannot, by definition, be random.
If you want a genuine random numbers, the randomness comes from atmospheric noise or radioactive decay.
You can try for example RANDOM.ORG (it reduces performance)
For strong random seed I always use CryptoRNG and not Time.
using System;
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
var random = new Random(GetSeed());
Console.WriteLine(random.Next());
}
public static int GetSeed()
{
using (var rng = new RNGCryptoServiceProvider())
{
var intBytes = new byte[4];
rng.GetBytes(intBytes);
return BitConverter.ToInt32(intBytes, 0);
}
}
}
Random rand = new Random();
int name = rand.Next()
Put whatever values you want in the second parentheses
make sure you have set a name by writing prop and double tab to generate the code
Random random = new Random ();
int randomNumber = random.Next (lowerBound,upperBound);
If you want a CSRNG to generate random numbers between a min and max, this is for you. It will initialize Random classes with secure random seeds.
class SecureRandom : Random
{
public static byte[] GetBytes(ulong length)
{
RNGCryptoServiceProvider RNG = new RNGCryptoServiceProvider();
byte[] bytes = new byte[length];
RNG.GetBytes(bytes);
RNG.Dispose();
return bytes;
}
public SecureRandom() : base(BitConverter.ToInt32(GetBytes(4), 0))
{
}
public int GetRandomInt(int min, int max)
{
int treashold = max - min;
if(treashold != Math.Abs(treashold))
{
throw new ArithmeticException("The minimum value can't exceed the maximum value!");
}
if (treashold == 0)
{
throw new ArithmeticException("The minimum value can't be the same as the maximum value!");
}
return min + (Next() % treashold);
}
public static int GetRandomIntStatic(int min, int max)
{
int treashold = max - min;
if (treashold != Math.Abs(treashold))
{
throw new ArithmeticException("The minimum value can't exceed the maximum value!");
}
if(treashold == 0)
{
throw new ArithmeticException("The minimum value can't be the same as the maximum value!");
}
return min + (BitConverter.ToInt32(GetBytes(4), 0) % treashold);
}
}
I will assume that you want a uniformly distributed random number generator like below. Random number in most of programming language including C# and C++ is not properly shuffled before using them. This means that you will get the same number over and over, which isn't really random. To avoid to draw the same number over and over, you need a seed. Typically, ticks in time is ok for this task. Remember that you will get the same number over and over if you are using the same seed every time. So try to use varying seed always. Time is a good source for seed because they chage always.
int GetRandomNumber(int min, int max)
{
Random rand = new Random((int)DateTime.Now.Ticks);
return rand.Next(min, max);
}
if you are looking for random number generator for normal distribution, you might use a Box-Muller transformation. Check the answer by yoyoyoyosef in Random Gaussian Variable Question. Since you want integer, you have to cast double value to integer at the end.
Random rand = new Random(); //reuse this if you are generating many
double u1 = 1.0-rand.NextDouble(); //uniform(0,1] random doubles
double u2 = 1.0-rand.NextDouble();
double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) *
Math.Sin(2.0 * Math.PI * u2); //random normal(0,1)
double randNormal =
mean + stdDev * randStdNormal; //random normal(mean,stdDev^2)
Random Gaussian Variables
Sorry, OP indeed requires a random int value, but for the simple purpose to share knowledge if you want a random BigInteger value you can use following statement:
BigInteger randomVal = BigInteger.Abs(BigInteger.Parse(Guid.NewGuid().ToString().Replace("-",""), NumberStyles.AllowHexSpecifier));
There are a number utility functions or services that are better cached in the same way that System.Random should be, so it lends itself to a generic implementation:
static public class CachedService<T> where T : new() {
static public T Get { get; } = new T();
}
To use for random (or similar):
CachedService<System.Random>.Get.Next(999);
Why not use int randomNumber = Random.Range(start_range, end_range) ?
Use one instance of Random repeatedly
// Somewhat better code...
Random rng = new Random();
for (int i = 0; i < 100; i++)
{
Console.WriteLine(GenerateDigit(rng));
}
...
static int GenerateDigit(Random rng)
{
// Assume there'd be more logic here really
return rng.Next(10);
}
This article takes a look at why randomness causes so many problems, and how to address them.
http://csharpindepth.com/Articles/Chapter12/Random.aspx
Random r=new Random();
int Numbers=r.next(min value, max value);
Try these simple steps to create random numbers:
Create function:
private int randomnumber(int min, int max)
{
Random rnum = new Random();
return rnum.Next(min, max);
}
Use the above function in a location where you want to use random numbers. Suppose you want to use it in a text box.
textBox1.Text = randomnumber(0, 999).ToString();
0 is min and 999 is max. You can change the values to whatever you want.
I always have methods that generate random numbers which help for various purposes. I hope this may help you too:
public class RandomGenerator
{
public int RandomNumber(int min, int max)
{
var random = new Random();
return random.Next(min, max);
}
public string RandomString(int size, bool lowerCase)
{
var builder = new StringBuilder();
var random = new Random();
char ch;
for (int i = 0; i < size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}
if (lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}
}
Quick and easy for inline, use bellow code:
new Random().Next(min, max);
// for example unique name
strName += "_" + new Random().Next(100, 999);
This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 7 years ago.
Consider this method:
private static int GenerateRandomNumber(int seed, int max)
{
return new Random(seed).Next(max);
}
On my machine, executing this loop yields the same number through 1500 iterations:
for (int i = 0; i < 1501; i++)
{
int random = GenerateRandomNumber(100000000, 999999999);
Console.WriteLine(random.ToString());
Console.ReadKey();
}
I get 145156561, for every single iteration.
I don't have a pressing issue, I was just curious about this behavior because .Next(max) says "Returns a Non Negative random number less than the specified maximum. Perhaps I am not understanding something basic.
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);
}
}
Dilbert has encountered the same problem back in 2001:
http://dilbert.com/strips/comic/2001-10-25/
Coincidence?
I don't think so.
And random.org agrees : http://www.random.org/analysis/
The problem is that you are creating a new Random instance with the same seed number each time. You should create a single Random instance (store it in a static if necessary) and simply call the next method on that same instance.
Random number generation is not truly random, see this Wikipedia entry for more details.
Salam to All,
Well it drove me crazy as well. The answer is simple. Change the seed before you generate random.
Example:
I want to generate random number between 1 to 10
Random rnd = new Random(DateTime.Now.Second);
int random_number = rnd.Next(10);
Put it inside a loop and run it three times. It will give out random numbers below 10.
Pseudo-random number generator usually work by choosing a seed, and then generating a deterministic sequence based on that seed. Choosing the same seed every time, you generate the same sequence.
There are "only" 2^32 different random sequences in .NET.
Not sure how the internals work.. check wiki for it, but it's very simple.
public class MathCalculations
{
private Random rnd = new Random();
public Int32 getRandom(Int32 iMin, Int32 iMax)
{
return rnd.Next(iMin, iMax);
}
}
public class Main
{
MathCalculations mathCalculations = new MathCalculations();
for (int i = 0; i < 6; i++)
{
getRandom(0,1000);
}
}
will generate Number1, Number2, Number3, Number4, Number5, Number6 (1 seed, 1 sequence of many numbers, random*not really, but approx.*)
if you however do this:
public class MathCalculations
{
public Int32 getRandom(Int32 iMin, Int32 iMax)
{
Random rnd = new Random();
return rnd.Next(iMin, iMax);
}
}
public class Main
{
MathCalculations mathCalculations = new MathCalculations();
for (int i = 0; i < 6; i++)
{
getRandom(0,1000);
}
}
You will now get Number1, Number1, Number1, Number1, Number1, Number1 (1 seed, 6 equal sequences of many numbers, always pick the same starting number from each equal sequence).. At some point Number1 will be different, because the seed changes over time.. but you need to wait some time for this, nonetheless, you never pick number2 from the sequence.
The reason is, each time you generate a new sequence with the same seed, hence the sequence is the same over and over again, and each time your random generated will pick the first number in it's sequence, which, with the same seed, is of course always the same.
Not sure if this is technically correct by the underlying methods of the random generator, but that's how it behaves.
In the event that anyone is looking for a "quick and dirty" "solution" (and I use that term with caution) then this will suffice for most.
int secondsSinceMidnight = Convert.ToInt32(DateTime.Now.Subtract(DateTime.Today).TotalSeconds);
Random rand = new Random(secondsSinceMidnight);
var usuallyRandomId = rand.Next();
Please note my use of usually random. I agree that the item marked as the answer is a more correct way of doing this.
This question already has answers here:
Random number generator only generating one random number
(15 answers)
Closed 5 years ago.
I'm using a method to create two new int arrays with random numbers,
but the two array contains exactly the same numbers. Why is this happening?
static void Main(string[] args)
{
int[] Foo1= Foo(1000);
int[] Foo2= Foo(1000);
}
static int[] Foo(int length)
{
int[] Array = new int[length];
Random r = new Random();
for (int i = 0; i < length; i++)
{
Array[i]=r.Next(1, 101);
}
//Thread.Sleep(6);
return Array;
}
You're not seeding Random but you're likely using it close enough between calls that the default seed is the same in both cases :
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. This problem can be
avoided by using a single Random
object to generate all random numbers.
You can also work around it by
modifying the seed value returned by
the system clock and then explicitly
providing this new seed value to the
Random(Int32) constructor. For more
information, see the Random(Int32)
constructor.
it's because your random is initialized twice, with a too small time difference to have a different seed.
try this :
static void Main(string[] args)
{
Random r = new Random();
int[] Foo1= Foo(1000,r);
int[] Foo2= Foo(1000,r);
}
static int[] Foo(int length, Random r)
{
int[] Array = new int[length];
for (int i = 0; i < length; i++)
{
Array[i]=r.Next(1, 101);
}
//Thread.Sleep(6);
return Array;
}
The other answers around "you're using two instances of Random with the same seed" are correct. However, they used to use a static variable to refer to an instance of Random. That could cause problems if you try to use it from multiple threads, because Random isn't thread-safe.
There are various workarounds for this (such as creating one instance of Random per thread, with a static method to access it safely) - I've written quite a long article on this topic which you may find useful.
you can use global Random object or you can use a good seed like
new Random(Guid.NewGuid().GetHashCode());
That is because of the Random class which are not really random numbers, but rather pseudo randoms. So each time a new Random instance is initialized in a short timespan, it begins with the same number again. If the timespan is bigger, you will have a different seed, so it would work. I would suggest to declare the Random instance in Main and hand it over to the Foo method, so it would look like this:
static void Main(string[] args)
{
Random r = new Random();
int[] Foo1= Foo(1000, r);
int[] Foo2= Foo(1000, r);
}
static int[] Foo(int length, Random r)
{
int[] Array = new int[length];
for (int i = 0; i < length; i++)
{
Array[i]=r.Next(1, 101);
}
//Thread.Sleep(6);
return Array;
}
Edit: I modified the code above after Jon Skeet's advice. Now the problem mentioned by him should be gone.
Because you are using almost the same seed. Try moving Random r = new Random(); outside of that method.
Ok so I have a dice throw app...
When I step through the code it functions normally and 'results' contains the correct number of throw results and they appear to be random, when I leave the code to run and do exactly the same thing it produces a set of identical numbers.
I'm sure this is a logical error I cannot see but fiddling with it for hours hasnt improved the situation, so any help is much appriciated. :)
class Dice
{
public int[] Roll(int _throws, int _sides, int _count)
{
Random rnd = new Random();
int[] results = new int[_throws];
// for each set of dice to throw pass data to calculate method
for (int i = 0; i < _throws; i++)
{
int thisThrow = Calculate(_sides, _count);
//add each throw to a new index of array... repeat for every throw
results[i] = thisThrow;
}
return results;
}
private int Calculate(int _sides, int _count)
{
Random rnd = new Random();
int[] result = new int[_count];
int total = 0;
//for each dice to throw put data into result
for (int i = 0; i < _count; i++)
{
result[i] = rnd.Next(1, _sides);
}
//count the values in result
for (int x = 0; x < _count; x++)
{
total = total + result[x];
}
//return total of all dice to Roll method
return total;
}
}
First mistake: Never use multiple instances of Random, use a single instance, and pass that along with the other parameters.
When you create "Random rnd = new Random();" it is seeded by the current time. When you debug your code (which takes time) it will be seeded differently each time.
Create 1 instance of Random, and reference that everywhere.
You're creating a random class every time you need to create a number. Doing this will give you the nutty results.
See here: FROM MSDN
This problem can be avoided by creating a single Random object rather than multiple ones.
To improve performance, create one Random object to generate many random numbers over time, instead of repeatedly creating a new Random objects to generate one random number.
E.g. create a private instance of Random...
In addition to what has been mentioned before...
Use Random for things like dice, card games, choosing random images and so forth. If you ever need to create a random number for security sake, use System.Security.Cryptography.RandomNumberGenerator. This simple example shows creating a random integer.
RandomNumberGenerator gen = RandomNumberGenerator.Create();
byte[] myBytes = new byte[4];
gen.GetBytes(myBytes);
int myValue = (BitConverter.ToInt32(myBytes, 0));
DO NOT use this unless you have a security need. The performance is less than that of the Random class. I suppose you could use this to seed Random but that might be overkill.
EDIT: It occurred to me that I had never tested this. A quick performance test showed the following:
1,000,000 random numbers:
RandomNumberGenerator: 2.6 seconds
Random: .015 seconds.
So Random is about 150 times faster.
Give the constructor Random a seed. That's the problem.
http://msdn.microsoft.com/en-us/library/aa329890%28VS.71%29.aspx
Random r = new Random(DateTime.Now.Millisecond);