0

The below code is copied from a paper, undergrad work was linked to from a wikipedia page. I believe I've spotted some flaws in the paper and some in the code but as I have no C# experience I just want to double check.

My understanding is that this code was meant to create a large psuedo totally random number but instead I believe it creates a large number which is a reoccuring smaller random number. ie. 123123123 instead of 123784675. Can someone please confirm what the code does.

What I read here http://csharpindepth.com/Articles/Chapter12/Random.aspx and on Stackoverflow in various posts makes me believe that it's using the same seed and hence getting the same number each iteration and just appending that same number over and over.

Random randomNumber = new Random();
counter = 0;

for (int j = 0; j < 1; j++)
{
    StringBuilder largeRandomNumber = new StringBuilder();
    for (int i = 0; i < 40000; i++)
    { 
        int value = randomNumber.Next(11111, 99999);
        largeRandomNumber.Append(value); 
    }
}
10
  • In just a few seconds, I ran your code and saw that wasn't the case... so -1 for not trying. Commented Oct 21, 2013 at 22:50
  • Looks completely fine to me... Take a look at compileonline.com/compile_csharp_online.php Commented Oct 21, 2013 at 22:50
  • @Austin Salonen. I should have said I can't try I don't have a C# compiler assumed that it wasn't available free being from MS and all. Commented Oct 21, 2013 at 22:56
  • 1
    The command line compiler is included in the framework. VS Express is free; Mono is free; there's likely a plug-in for Eclipse -- the options exist. Commented Oct 21, 2013 at 22:57
  • 1
    @RhuaidhriTynan That means the following: Random r = new Random(1); r.next(0,10) // will return "2" This will ALWAYS return 2 (even in 50 years!), cause thats, what "seed 1" will return! Use "no seed", then C# will take the "micro-time" which will Never ever be the Same number, unless you manage to generate the next number within the same micro second than the previous. Commented Oct 21, 2013 at 23:09

5 Answers 5

1

Creates instance of random and loops through creating the next random number using random.Next(int min, int max) and appends this on the end of a string. Essentially, it just creates one huge number for something. Outer loop is garbage, not needed at all. Random doesn't need to be seeded again after creation...keeps same seed and progresses correctly using the Next method. Everything about this code "works" but seems pointless in any application besides learning about the random class.

Sign up to request clarification or add additional context in comments.

Comments

1

The code is fine.

Try it here: http://www.compileonline.com/compile_csharp_online.php

using System.IO;
using System;
using System.Text;

class Program
{
    static void Main()
    {
       Random randomNumber = new Random();

       for (int j = 0; j < 1; j++)
       {
           StringBuilder largeRandomNumber = new StringBuilder();    

           for (int i = 0; i < 40; i++)
           { 
               int value = randomNumber.Next(11111, 99999);
               Console.WriteLine(value); 
            }
        }
    }
}

Comments

1

It is correct. Yes, Seed is same but this line will make sure that you get different number on different run :

int value = randomNumber.Next(11111, 99999);

and since you are appending this number to a string to create large random number, this does what it was supposed to do.

Comments

0

randomNumber is seeded on construction. randomNumber.Next is returning the next random integer between the two given integers based on the initial seed, and is not reseeded, thus giving 40000 new random numbers and appending them.

Not sure what the outer loop is for, it only runs once anyway

Comments

0

Yes, it is most likely that it will generate the same number if called quickly in succession, since Random is seeded with the current time. Unless they instantiated the randomNumber instance inside the loop however, for purposes of an example it works fine.

For example, if the code is plugged into a function like this

    public string GetLargeRandomNumber()
    {
        Random randomNumber = new Random();
        StringBuilder largeRandomNumber = new StringBuilder();

        for (int j = 0; j < 1; j++)
        {
            for (int i = 0; i < 40000; i++)
            {
                int value = randomNumber.Next(11111, 99999);
                largeRandomNumber.Append(value);
            }
        }
        return largeRandomNumber.ToString();
    }

And called from a main function in quick succession it will return the same random number.

2 Comments

If you run the code in a function it will instantiate randomNumber with the same seed, thus returning the same number. Therefore, context is important.
Granted deleting comment and upvoting (sorry)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.