开发者

random number gen, loop and store

How can I implement this so it is always ten numbers in length and how would I store n and r, loop 10 times, and store each loop aswell?

loop the random generator 10x to create 2x10 multiple 10 digit numbers.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {

            F();
            F();
        }

        static Random _r = new Random();
        static void F()
        {

            int n = _r.Next();
            int r = _r.Next();

            Console.WriteLine(n);
            Console.WriteLi开发者_如何学运维ne(r);
            Console.ReadLine();
        }
}


Something like this may be what you're looking for?

Though I'm not keen on the naming conventions, I'm not sure what these apply to, so can't exactly name them relevantly myself...

static int[] randomNumberArray0 = new int[10];
static int[] randomNumberArray1 = new int[10];
static Random random = new Random();

static void PopulateRandomNumberArrays()
{
    for (int i = 0; i < 10; i++)
    {
        randomNumberArray0[i] = random.Next();
        randomNumberArray1[i] = random.Next();
    }
}

Update:

To execute this method then further output the values at a later time, try this:

static void PrintRandomNumberArrayValues(int[] array)
{
    for (int i = 0; i < array.Length; i++)
    { 
        Console.WriteLine("{0}", array[i]);
    }
    Console.WriteLine();
}

static void Main()
{
    PopulateRandomNumberArrays();

    PrintRandomNumberArrayValues(randomNumberArray0);
    PrintRandomNumberArrayValues(randomNumberArray1);

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
}

Output would look something like this:

2044334973
153458690
1271210885
734397658
746062572
162210281
1091625245
123317926
410432738
989880682

866647035
481104609
834599031
1153970253
94252627
1041485031
1934449666
414036889
1886559958
2083967380

Further update:

For the generated values to be constrained to 10 digits in length, it means the minimum generated value must be larger than 999999999 and, since an int has a max value of 2,147,483,647, smaller or equal to that, so:

for (int i = 0; i < 10; i++)
{
    randomNumberArray0[i] = random.Next(1000000000, int.MaxValue);
    randomNumberArray1[i] = random.Next(1000000000, int.MaxValue);
}

I have a feeling this still may not satisfy your needs though, as what I see coming next is that the numbers should be able to be represented as 0000000001, for instance - let's see.

Yet another update:

As I thought, the value of numbers should not be constrained to our specified range, but rather only the output formatted appropriately; thus generation reverts to my original suggestion and printing is altered as so:

Console.WriteLine("{0:D10}", array[i]);


I guess you could use a list of tuples to store and return your results:

static List<Tuple<int,int>> F()
{
    var results = new List<Tuple<int,int>> ();
    for (int i = 0; i < 10; i++)
    {
        results.Add(new Tuple<int, int>(_r.Next(), _r.Next()));
    }
    return results;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜