1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【C#備忘録】ランダムな値を出力する

Last updated at Posted at 2020-02-24

※この備忘録は初学者の私が学習した内容を忘れないために書き起こしたものとなります。

①Randomクラスのインスタンスを生成する

using System;
public class Program{
    public static void Main(){
        var random = new Random();            
    }
}

上記のように、変数randomにRandomクラスのインスタンスを生成し代入する。

②変数randomを、Nextメソッド、WriteLineメソッドを用いて表示させる

using System;
public class Program{
    public static void Main(){
        var random = new Random();
        var number = random.Next(1,25);  //1以上25未満の数値を指定
        Console.WriteLine("C#を1日に" + number + "時間勉強した");            
    }
}

※「C#を1日に【1~24の中のランダム値】時間勉強した」と出力される

注意点

Nextメソッドの引数を指定しないととんでもない桁数の数値からランダムで出力されてしまう。
そのため、Next(〇〇,△△)のように引数を指定するとよい。
また、引数は、〇〇以上、△△未満を意味している。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?