tags:

views:

88

answers:

2

I'd like to know a good way to generate several megabytes of random data using C# on Windows (using .NET 2.0 for deployment, but other approaches using other options such as .NET 3.0 and Mono libraries would also be of some interest).

The purpose of the data is to transmit over a network connection, to evaluate real world network throughput to fixed destinations, so the level of randomness is not really significant - though the data itself should not be something trivially compressible (such as simple series of repeating characters).

I can use Random, or generate GUID's but I'm curious to know if there are better ways of doing this. Is there something analogous to /dev/urandom on UNIX for example?

+2  A: 

You can use this approach to generate random numbers stream:

int streamSize = (1024 * 1024) * 15; // Let's make 15 Mbytes of junk

int left = streamSize;

while (left > 0)
{
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider ();
    byte[] buffer = new byte[1024];
    rng.GetBytes (buffer);

    SendBytes (buffer); // Send it to network interface

    left -= 1024;
}
Developer Art
That's spot on thanks. I didn't think of looking at the crypto API's (though it seems obvious now, duh).
Iain Collins
The crypto APIs are really just giving you a more "secure" type of randomness. Using System.Random would almost certainly be good enough really.
Jon Skeet
Note that crypto-strength randomness does not make entropy out of nothing. That entropy has to come from somewhere. If you are generating millions or billions of bytes of crypto-strength randomness it can get very slow; the stream will only produce new bits when there's enough entropy accumulated to ensure crypto-strength randomness. If you don't require crypto-strength randomness I would just go with pseudo-randomness; that doesn't depend on being able to find more entropy lying around the system.
Eric Lippert
Of course, any point about performance is meaningless without numbers. On my machine I seem to be able to generate about ten megs of crypto randomness per second in the steady state. Odds are good that's acceptable performance if you're just throwing it at a network, which is going to have latency anyway.
Eric Lippert
Hey Eric, yeah crytpo-strength randomness is not needed here, but the performance issue is worth noting. It's only a few megabytes but I wanted to see if there are options that are more suitable than just System.Random for generating large amounts of data (will need to take a look a both in practice to see which seems to cope the best).
Iain Collins
A: 

Developer Art's suggestion is good for getting pretty random data - but in real life the data may well not be completely random. I appreciate that it shouldn't be trivially compressible - but do you really want it to be completely non-compressible?

Real world network data consists of a variety of types of traffic - some will be compressible, some won't.

To my mind, the best way of getting realistic data is to sample it from the real world. What kind of traffic will you be getting in real life? Are you able to sample it from real users - potentially a variety of different users?

Jon Skeet