計算機科学のブログ

objects…get oriented! Making code make sense - Class, methods, instances, array, for loop

Head First C#: A Learner’s Guide to Real-World Programming with C# and .NET Core (Andrew Stellman(著)、Jennifer Greene(著)、O’Reilly Media)のChapter 3(objects…get oriented! Making code make sense)、p.131(Sharpen your pencil)の解答を求めてみる。

コード

using System;

namespace MyFirstConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Random random = new Random();
            // Random
            // ...
            // Next
            // NextBytes
            // NextDouble
            // ...
            double[] randomDoubles = new double[20];
            for (int j = 0; j < 5; j++)
            {
                Console.WriteLine($"j = {j}");
                for (int i = 0; i < 20; i++)
                {
                    double value = random.NextDouble();
                    randomDoubles[i] = value;
                }
                foreach (var item in randomDoubles)
                {
                    Console.WriteLine(item);
                }
            }
        }
    }
}

入出力結果(Terminal)

j = 0
0.353472783860505
0.3023968806035802
0.4479394701532738
0.41025703838572697
0.9993309346024557
0.7465561552655678
0.80362212322821
0.9821429108186359
0.5676574583946064
0.7026648254611831
0.03608803592440115
0.5299086461448617
0.875105671992109
0.11559242946821843
0.8781532453737004
0.3134199889904912
0.8733188765464904
0.303059337336132
0.14413933695486716
0.2835635381208563
j = 1
0.13745816896551202
0.3784353772077874
0.8635268410963597
0.16521090090517462
0.10368940518409452
0.20673258332849134
0.046936815160762896
0.6642395079435033
0.9296266417622691
0.23590420337203155
0.5110336027625173
0.7989886821242927
0.6128277534678708
0.1864761245373991
0.028463103356055498
0.05118471945225481
0.3923257586510506
0.6956516526153552
0.9650408350699772
0.9181552049322776
j = 2
0.4698918352229017
0.021442937674672778
0.6709697594265313
0.5002173229587346
0.2115092692950318
0.2005115189591942
0.5853341941653445
0.9848866579052464
0.35804903477339495
0.6892355604512782
0.2965806095379314
0.8389245499106704
0.964490624128138
0.3313827623293655
0.23251103341230706
0.9750374066527175
0.43887003950722053
0.28272856924809914
0.3065676332016325
0.7925983512739643
j = 3
0.699619340104805
0.13938261528470675
0.05251626905636688
0.3317532550225748
0.19163122269866578
0.23709935380010835
0.9170808926769909
0.68862954745471
0.08712932611216294
0.8269685259214455
0.9210942303394406
0.17766722393113524
0.3380185022661549
0.22598413202258952
0.8136717028979545
0.11601523129083925
0.7074656177812562
0.3633095181376252
0.9537016316101429
0.9031778862249004
j = 4
0.6213983891631469
0.062050157255516464
0.3061904731701084
0.24039108131099077
0.9393235938341001
0.672109052851847
0.8344980579961548
0.2814449911385053
0.9539650911250921
0.053425696703337924
0.6123146799450343
0.10959718940295148
0.38908401941372267
0.17244248379601282
0.21853586482747264
0.33050921993819493
0.9689266686183059
0.33921650440395645
0.3085861002600687
0.9744099154949234