开发者

.NET: Random number or number of free bytes in memory?

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?


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...


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜