# Create an array with random numbers

private static byte[] GenerateData(long size)
{
var result = new byte[size];
new Random().NextBytes(result);

return result;
}


Is it good, robust and efficient solution?

I use my method like this

[TestMethod]
{
var length = 1024 * 1024 * 16; // 16mb
}

private void WriteAndReadTest(long length, long offset = 0L)
{
byte[] generatedData = GenerateData(length);
byte[] expectedData = null;

StorageException resultException = null;
try
{
}
catch (StorageException exception)
{
resultException = exception;
}

Assert.IsNull(resultException);
Assert.IsTrue(generatedData.Length == expectedData.Length);
Assert.IsTrue(generatedData.SequenceEqual(expectedData));
}

• For what purpose? A card game has different random number requirements than an initialization vector for an encryption algorithm, for example. – Jesse C. Slicer Apr 26 '16 at 16:01
• – Heslacher Apr 26 '16 at 16:06
• @JesseC.Slicer This array must represent a file with a particular size – Anatoly Apr 26 '16 at 16:08
• @Anatoly ok, so down to the next level: What does the file represent? Is the random data for use in the future? Or is it throwaway? Or is it supposed to overwrite unused areas of a hard drive securely? – Jesse C. Slicer Apr 26 '16 at 16:13
• Yes, it is throwaway. I will use this method inside my tests class – Anatoly Apr 26 '16 at 16:17

I will use this method inside my tests class

Your plan is to generate random data to test your thing. Suppose a test fails. How are you planning on debugging it with the data that caused it to fail?

Don't randomize tests. Generate the random test data once, and save it, and then use it over and over again.

• Great idea, thanks. Generate the random test data once, and save it. I don't understand how to do this, can you explain please? – Anatoly Apr 27 '16 at 5:24
• I'm using C# and NUnit. I have a 10 unit tests. Previously I decided to call my GenerateData(specific_for_a_test) inside each of tests, but with different size (1MB, 100MB, 1GB). So how can I use your recommendation in my case? Can you provide a code please – Anatoly Apr 27 '16 at 5:51
• I update my answer, and provide more code – Anatoly Apr 27 '16 at 6:02
• @Anatoly You can use a PRNG together with a seed so that you will get the same test data each time your test is run. Note that "Random objects in processes running under different versions of the .NET Framework may return different series of random numbers even if they're instantiated with identical seed values", so you should keep that in mind (and consider using a different PRNG). – mjolka Apr 27 '16 at 8:01

You don't need to new up a new random number generator every time. In fact, doing so may result in the same sequence of "random" numbers being generated if they're called too fast in succession (it bases itself off of ticks of the real-time clock). However, with a single instance of the RNG, you wind up with a similar issue as it's not thread-safe, so a mutual exclusion scheme needs to be used to keep its calls serialized. So, in the snippet below, there's one RNG created at the class level and then the lock keyword is used to ensure mutual exclusion from multiple threads.

private static readonly Random _Random = new Random();

private static byte[] GenerateData(long size)
{
var result = new byte[size];

lock (_Random)
{
_Random.NextBytes(result);
}

return result;
}

• Maybe GenerateData() doesn't need to be static? In that case, you wouldn't need the lock. – svick May 1 '16 at 1:13
• I'm pretty sure its staticness doesn't determine whether or not it needs the lock - it's possible simultaneous access to the PRNG. Could be multiple readers in a non-static instance. – Jesse C. Slicer May 1 '16 at 1:17
• static methods almost always have to be thread-safe, instance methods usually don't (since each thread can have its own instance). – svick May 1 '16 at 1:18
• Can, yes. But it has to be designed as such. Perhaps it's time to link to Jon Skeet's Random helper: codeblog.jonskeet.uk/2009/11/04/revisiting-randomness – Jesse C. Slicer May 1 '16 at 1:21