-3

After studiously procrastinating I got to wondering about Random numbers and how no one seems satisfied with the randomness of the Random number generator and came up with the below code. Would this be more random than Random?

Note: I'm just playing here, that is to say: not a serious question and definitely not worth spending more than an hour answering.

        Random rand = new Random();
        List<int> fifteenRandomNumbers = new List<int>();
        for (int i = 0; i < 15; i++)
        {
            int randomNumber = rand.Next(0, 10);
            fifteenRandomNumbers.Add(randomNumber);
        }
        string allNumbers = "";
        foreach (int number in fifteenRandomNumbers)
        {
            allNumbers += number;
        }
        Convert.ToUInt64(allNumbers);

UPDATE: Thanks for the quick answers , and just to reiterate, this isn't something I'm aiming at actually implementing , I was just curious. And I'm not really worried about if it would work, it's more about the broader concept of random numbers. I'm also not considering cryptography, as interesting as it may be. I'm also not worried about casting etc.

Just to explain my thinking here (which has been pointed out to be entirely wrong but that's kind of why I asked) I figured that if I joined a series of results from 15 Random.Next(0,n) operations the odds of getting a truly random number would be greater than if I'd done only one such operation. This has been said to be a false assumption and I appreciate the time spent answering my question.

Dani
  • 2,480
  • 3
  • 21
  • 27
  • 2
    I would say no. No value is unique in a finite number system. – Daniel A. White Oct 11 '11 at 14:53
  • 1
    Er, no. It will most likely be exactly as random as Random, including having the same "tight loop all the same values" likelihood. – Marc Gravell Oct 11 '11 at 14:54
  • Might be a little more random than normal random, but it's how the random number is generated that stops it from being truly random. Your version just melds some together. – tbddeveloper Oct 11 '11 at 14:54
  • 5
    That will almost always give an exception due to int.MaxValue only having 10 digits.... – Jon Skeet Oct 11 '11 at 14:54
  • If you want "proper" randomness, look at the crypto classes; that is all. – Marc Gravell Oct 11 '11 at 14:56
  • 2
    ["Random numbers should not be generated with a method chosen at random." — Donald Knuth](http://www.random.org/quotations/) – AakashM Oct 11 '11 at 14:56
  • It would be the same as Random, because your solution is based on the same Random implementation as the original one. And you should use a string builder, because the string is immutable. And wouldn't even work because Int32 value type represents signed integers with values ranging from negative 2,147,483,648 through positive 2,147,483,647. – BigL Oct 11 '11 at 14:58
  • Yeah, after actually running it I had an Overflow Exception (obviously), should've done that before posting I suppose. – Dani Oct 11 '11 at 14:58
  • Related: http://stackoverflow.com/questions/1257299/why-use-the-c-class-system-random-at-all-instead-of-system-security-cryptography – H H Oct 11 '11 at 15:09
  • 1
    If you want something more random I'd suggest hooking up a Geiger counter to your PC and shoving some radium underneath it. – RichK Oct 11 '11 at 15:11

1 Answers1

2

No, it would not be any more random, and no, it would not even fit into the range of Int32. 999,999,999,999,999 (15 digits) > int.MaxValue.

If you are not satisified with the randomness of Random, it's quite possible you are (a) using it wrong or (b) need something more cryptographically secure.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • or (c) need a better PRNG algorithm such as a [Mersenne Twister](http://en.wikipedia.org/wiki/Mersenne_twister) (Crypto-PRNGs are designed to meet Cryptographioc requirements, other uses of a PRNG might not match. – Richard Oct 11 '11 at 15:05