LoginSignup
0
1

More than 5 years have passed since last update.

VBA VBS System Random Class

Last updated at Posted at 2019-02-23

関数リスト

関数名 説明
RndINT32GenNext 0からInt32型の最大値=Long型の最大値2147483647より小さい整数の乱数を生成
rngGen0Max(LongMaxNumber As Long) 0以上LongMaxNumberより小さい整数の乱数を返す。ただし最大値2147483647より大きい場合や、マイナスや1より小さい場合は0を返す。
RndINT32GenInterval(LongFromNumber As Long, LongMaxNumber As Long) LongFromNumber以上LongMaxNumber未満の整数。ただし最大値は上記と同じ。
RndGen0To1 0.0以上1.0未満の倍精度浮動小数点数

VBA mscorelib参照設定版Class

mscorelib.tlbへの参照設定が必要です

Option Explicit
' Class Name = "cls_Genelic_Rnd"
' Ver20190223
Private Const MaxRandomInt16 As Long = 2147483647
Private mscoreLibInt32Rnd
Private Sub Class_Initialize()
Set mscoreLibInt32Rnd = New mscorlib.Random
'Set mscoreLibInt32Rnd = CreateObject("System.Random")
End Sub
Function rndgenNext()
rndgenNext = mscoreLibInt32Rnd.Next()
End Function

Function RndINT32NumberGen0toMax(LongMaxNumber As Long)
If LongMaxNumber <= MaxRandomInt16 Then
RndINT32NumberGen0toMax = mscoreLibInt32Rnd.Next_3(LongMaxNumber)
ElseIf LongMaxNumber < 0 Then
Debug.Print "RndINT32NumberGen0toMax error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。マイナスなので0のみ返します"
RndINT32NumberGen0toMax = 0
ElseIf LongMaxNumber < 1 And LongMaxNumber >= 0 Then
Debug.Print "RndINT32NumberGen0toMax error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。1より小さい数は生成できないので、0を返します"
RndINT32NumberGen0toMax = 0
End If
End Function

Function RndINT32GenInterval(LongFromNumber As Long, LongMaxNumber As Long)
If LongFromNumber < LongMaxNumber And LongMaxNumber <= MaxRandomInt16 Then
RndINT32GenInterval = mscoreLibInt32Rnd.Next_2(LongFromNumber, LongMaxNumber)
ElseIf LongMaxNumber < 0 Then
Debug.Print "RndINT32GenInterval error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。マイナスなので0のみ返します"
RndINT32GenInterval = 0
ElseIf LongMaxNumber < 1 And LongMaxNumber >= 0 Then
Debug.Print "RndINT32GenInterval error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。1より小さい数は生成できないので、0を返します"
RndINT32GenInterval = 0
End If
End Function
Function RndGen0To1() As Double
'0.0以上1.0未満の倍精度浮動小数点数
RndGen0To1 = mscoreLibInt32Rnd.Nextdouble
End Function
Private Sub Class_Terminate()
If Not mscoreLibInt32Rnd Is Nothing Then Set mscoreLibInt32Rnd = Nothing
End Sub

VBA CreateOjbect版 Class

参照設定なしで動きます。

Option Explicit
' Class Name = "cls_Genelic_Rnd"
' Ver20190223
Private Const MaxRandomInt16 As Long = 2147483647
Private mscoreLibInt32Rnd

Private Sub Class_Initialize()
Set mscoreLibInt32Rnd = New mscorlib.Random
'Set mscoreLibInt32Rnd = CreateObject("System.Random")
End Sub

Function rndgenNext()
rndgenNext = mscoreLibInt32Rnd.Next()
End Function

Function RndINT32NumberGen0toMax(LongMaxNumber As Long)
If LongMaxNumber <= MaxRandomInt16 Then
RndINT32NumberGen0toMax = mscoreLibInt32Rnd.Next_3(LongMaxNumber)
ElseIf LongMaxNumber < 0 Then
Debug.Print "RndINT32NumberGen0toMax error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。マイナスなので0のみ返します"
RndINT32NumberGen0toMax = 0
ElseIf LongMaxNumber < 1 And LongMaxNumber >= 0 Then
Debug.Print "RndINT32NumberGen0toMax error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。1より小さい数は生成できないので、0を返します"
RndINT32NumberGen0toMax = 0
End If
End Function

Function RndINT32GenInterval(LongFromNumber As Long, LongMaxNumber As Long)
If LongFromNumber < LongMaxNumber And LongMaxNumber <= MaxRandomInt16 Then
RndINT32GenInterval = mscoreLibInt32Rnd.Next_2(LongFromNumber, LongMaxNumber)
ElseIf LongMaxNumber < 0 Then
Debug.Print "RndINT32GenInterval error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。マイナスなので0のみ返します"
RndINT32GenInterval = 0
ElseIf LongMaxNumber < 1 And LongMaxNumber >= 0 Then
Debug.Print "RndINT32GenInterval error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。1より小さい数は生成できないので、0を返します"
RndINT32GenInterval = 0
End If
End Function

Function RndGen0To1() As Double
'0.0以上1.0未満の倍精度浮動小数点数
RndGen0To1 = mscoreLibInt32Rnd.Nextdouble
End Function

Private Sub Class_Terminate()
If Not mscoreLibInt32Rnd Is Nothing Then Set mscoreLibInt32Rnd = Nothing
End Sub

VBA 標準モジュール

Sub classGenericRandomSample()
Dim cRnd As New cls_Genelic_Rnd: Set cRnd = New cls_Genelic_Rnd
Debug.Print cRnd.RndINT32GenNext
Debug.Print cRnd.RndINT32NumberGen0toMax(10000)
Debug.Print cRnd.RndINT32GenInterval(200, 400)
Debug.Print cRnd.RndGen0To1
Set cRnd = Nothing
End Sub

VBScript版

Option Explicit
Const MaxRandomInt16 = 2147483647

Class cls_Genelic_Rnd
' Class Name = "cls_Genelic_Rnd"
' Ver20190223

Private mscoreLibInt32Rnd

Private Sub Class_Initialize()
Set mscoreLibInt32Rnd = CreateObject("System.Random")
End Sub

Function RndINT32GenNext()
RndINT32GenNext = mscoreLibInt32Rnd.Next()
End Function

Function RndINT32NumberGen0toMax(LongMaxNumber)
If LongMaxNumber <= MaxRandomInt16 Then
RndINT32NumberGen0toMax = mscoreLibInt32Rnd.Next_3(LongMaxNumber)
ElseIf LongMaxNumber < 0 Then
Wscript.Echo "RndINT32NumberGen0toMax error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。マイナスなので0のみ返します"
RndINT32NumberGen0toMax = 0
ElseIf LongMaxNumber < 1 And LongMaxNumber >= 0 Then
Wscript.Echo "RndINT32NumberGen0toMax error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。1より小さい数は生成できないので、0を返します"
RndINT32NumberGen0toMax = 0
End If
End Function

Function RndINT32GenInterval(LongFromNumber, LongMaxNumber)
If LongFromNumber < LongMaxNumber And LongMaxNumber <= MaxRandomInt16 Then
RndINT32GenInterval = mscoreLibInt32Rnd.Next_2(LongFromNumber, LongMaxNumber)
ElseIf LongMaxNumber < 0 Then
Wscript.Echo  "RndINT32GenInterval error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。マイナスなので0のみ返します"
RndINT32GenInterval = 0
ElseIf LongMaxNumber < 1 And LongMaxNumber >= 0 Then
Wscript.Echo "RndINT32GenInterval error occur. この関数は0以上LongMaxNumber(長整数型,最大値2147483647)より小さい正の整数を返す関数です。1より小さい数は生成できないので、0を返します"
RndINT32GenInterval = 0
End If
End Function

Function RndGen0To1()
'0.0以上1.0未満の倍精度浮動小数点数
RndGen0To1 = mscoreLibInt32Rnd.Nextdouble
End Function

Private Sub Class_Terminate()
If Not mscoreLibInt32Rnd Is Nothing Then Set mscoreLibInt32Rnd = Nothing
End Sub

End Class

Dim cRnd : Set cRnd = New cls_Genelic_Rnd
Wscript.Echo cRnd.RndINT32GenNext
Wscript.Echo cRnd.RndINT32NumberGen0toMax(10000)
Wscript.Echo cRnd.RndGen0To1
Wscript.Echo cRnd.RndINT32GenInterval(200, 400)

Set cRnd = Nothing

参考文献

'Randomクラスで乱数を作る
'>Nextメソッド:0 以上の乱数を返します。
'>Next_3メソッド:指定した最大値より小さい 0 以上の乱数を返します。
'>Next_2メソッド: 指定した範囲内の乱数を返します。
'>NextDoubleメソッド:0.0 と 1.0 の間の乱数を返します。
'Random Class
'Hey, Scripting Guy!: Be Careful What You Say(http://technet.microsoft.com/ja-jp/magazine/2007.01.heyscriptingguy.aspx)から移動
'Windows API Catalog へ移動したらしい http://msdn.microsoft.com/library/en-us/cpref/html/cpref_start.asp

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