views:

78

answers:

3

How can I get the number of free bytes available in memory, in .NET?

Also, can you think of a potentially better random number beside new Random().Next(), DateTime.Now.Ticks, or available system memory?

+2  A: 

That might work better as 2 questions...

Re new Random().Next() - that will obviously die in a tight loop, so an existing random.Next() would be more random ;-p But you perhaps want a cryptographic random - something like:

    var random = new RNGCryptoServiceProvider();
    byte[] bytes = new byte[4];
    random.GetBytes(bytes);
    int i = BitConverter.ToInt32(bytes, 0);

Note that this makes testing harder though, as it can't be seeded etc...

Marc Gravell
+1  A: 

Why re-invent the wheel when there is a very nice one available here: Random.org ?

RANDOM.ORG offers true random numbers to anyone on the Internet. The randomness comes from atmospheric noise, which for many purposes is better than the pseudo-random number algorithms typically used in computer programs. People use RANDOM.ORG for holding drawings, lotteries and sweepstakes, to drive games and gambling sites, for scientific applications and for art and music.

Disclaimer: I am not affiliated in any way with this site, nor can I vouch for the quality of what they offer. I just personally think it is a pretty cool idea.

JohnFx
A: 

As for your question regarding memory metrics, look into using performance counters.

using System.Diagnostics;
...
PerformanceCounter counter = new PerformanceCounter("Memory", "Available Bytes");
long freeBytes = counter.RawValue;

You may end up needing to use a different counter, such as "Memory\Free & Zero Page List Bytes," to get what you want. I recommend experimenting with the performance counters in perfmon.

Matthew Cole