5

The c#/XNA process for creating random numbers is pretty quick and easy, however, it is quite possibly the worst distributing random number generator I have ever seen. Is there a better method that is easy to implement for c#/XNA?

rand.Next() just doesn't suit my needs.

from:

static private Random rand = new Random();

I randomly place objects, all over my program. sometimes 10, sometimes 200.

When calling for random objects (the x val and y val are both random on a 2d plane), they group. The generation code is clean and calls them good, iterated cleanly and pulls a new random number with each value. But they group, noticeably bad, which isn't very good with random numbers after all. I'm intermediate skill with c#, I crossed over from as3, which seemed to handle randomness better.

I am well-aware that they are pseudo random, but C# on a windows system, the grouping is grotesque.

SimpleRookie
  • 305
  • 1
  • 3
  • 14

3 Answers3

2

Can you use System.Security.Cryptography.RandomNumberGenerator from XNA?

var rand = RandomNumberGenerator.Create();
byte[] bytes = new byte[4];

rand.GetBytes(bytes);

int next = BitConverter.ToInt32(bytes, 0);

To get a value within a min/max range:

static RandomNumberGenerator _rand = RandomNumberGenerator.Create();

static int RandomNext(int min, int max)
{
    if (min > max) throw new ArgumentOutOfRangeException("min");

    byte[] bytes = new byte[4]; 

    _rand.GetBytes(bytes);

    uint next = BitConverter.ToUInt32(bytes, 0);

    int range = max - min;

    return (int)((next % range) + min);
}
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
  • How would one set the min and max for that method? – SimpleRookie Oct 03 '11 at 00:20
  • 1
    Could you give an example of what your min/max values are? – Jeff Ogata Oct 03 '11 at 00:41
  • 0-10, 1-200, -100-100, standard stuff. – SimpleRookie Oct 03 '11 at 00:50
  • 1
    @SimpleRookie - please see my edit. I added code to get a random number within a range. I'm curious to see if the distribution is better than the standard `Random.Next`. – Jeff Ogata Oct 03 '11 at 01:23
  • Yes. It is MUCH better in all instances I have tested. – SimpleRookie Oct 03 '11 at 03:21
  • That was a really good substitution, thank you very much. I wish I could upvote your answer a few more times. The spread of results from your solution blows the common Random.Next out of the water. – SimpleRookie Oct 03 '11 at 04:40
  • Added Information: I have noted a small bit a trouble that this solution gives me is with coinflips. 0,1 calls will only return one value. So I have to step it up to (0,2) too get a proper response from the function, weird little tibbit that I am sure someone much smarter then I could comment on. – SimpleRookie Oct 21 '11 at 20:29
1

For my project I use simple XOR algorythm. I don't know how good it distributes numbers, but it is easy to implement:

http://www.codeproject.com/KB/cs/fastrandom.aspx

1

It may also depend on how you're using System.Random. As a general rule, it is not a good idea to create new instances of Random over and over, as Random objects created at about the same time will very likely be seeded with the same random number (by default, time is used as the seed.) Instead, create a single instance of Random and use just that instance throughout your program.

Unless the goal is security, which is probably not the case here, you will be better off just using System.Random.

Peter O.
  • 32,158
  • 14
  • 82
  • 96