0
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?

ランダムな数値、日付

Posted at

はじめに

サンプルデータを作る為、ランダムな数値、日付が大量に必要になりました。
データはExcelで作成しDBに投入しましたが、C#でも作成してみました。

Excel

Excel数式
>a以上b未満の整数
=ROUNDDOWN(RAND()*(b-a)+a,0)

>0.0以上1.0未満の浮動点小数
=RAND()

>2000-01-01~2025-12-31
=DATE(2000,1,1)+ROUNDDOWN(RAND()*(DAYS(DATE(2025,12,31),DATE(2000,1,1))+1),0)

C#

C#では「Random」を使う方法と「RNGCryptoServiceProvider」を使う方法があるようですが、厳密さは求められていないので、「Random」を使いました。

C#
var r = new Random();

// 整数
int intRandom1 = r.Next(); // 0~111(0以上)
int intRandom2 = r.Next(10); // 0~9(0以上10未満)
int intRandom3 = r.Next(-5, 10); // -5~9(-5以上10未満)

// 小数
double valRandom = r.NextDouble(); // 0.0以上1.0未満の浮動点小数

// 日付
var ds = new DateTime(2000, 1, 1);
var de = new DateTime(2025, 12, 31);
DateTime d = ds.AddDays(r.Next((de - ds).Days) + 1); // 2000-01-01~2025-12-31
0
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
0
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?