1 #Region "Microsoft.VisualBasic::0e217d850406a5e9fbd43243f8f810c4, Microsoft.VisualBasic.Core\Language\Language\C\RandomNumbers.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module RandomNumbers
35     
36     '         Constructor: (+1 OverloadsSub New
37     
38     '         Function: rand, random
39     
40     '         Sub: randomize, srand
41     
42     
43     ' /********************************************************************************/
44
45 #End Region
46
47 '----------------------------------------------------------------------------------------
48 ' Copyright © 2006 - 2012 Tangible Software Solutions Inc.
49 ' This class can be used by anyone provided that the copyright notice remains intact.
50 ' ' This class provides the ability to simulate the behavior of the C/C++ functions for 
51 ' generating random numbers, using the .NET Framework System.Random class.
52 ' 'rand' converts to the parameterless overload of NextNumber
53 ' 'random' converts to the single-parameter overload of NextNumber
54 ' 'randomize' converts to the parameterless overload of Seed
55 ' 'srand' converts to the single-parameter overload of Seed
56 '----------------------------------------------------------------------------------------
57
58 Namespace Language.C
59
60     ''' <summary>
61     ''' This class provides the ability to simulate the behavior of the C/C++ functions for 
62     ''' generating random numbers, using the .NET Framework <see cref="System.Random"/> class.
63     '''
64     ''' + ``rand`` converts to the parameterless overload of NextNumber
65     ''' + ``random`` converts to the single-parameter overload of NextNumber
66     ''' + ``randomize`` converts to the parameterless overload of Seed
67     ''' + ``srand`` converts to the single-parameter overload of Seed
68     ''' </summary>
69     Public Module RandomNumbers
70
71         Dim r As Random
72
73         Sub New()
74             Call randomize()
75         End Sub
76
77         ''' <summary>
78         ''' <see cref="System.Random.Next"/>.(线程安全的函数)
79         ''' </summary>
80         ''' <returns></returns>
81         Public Function rand() As Integer
82             SyncLock r
83                 Return r.[Next]()
84             End SyncLock
85         End Function
86
87         ''' <summary>
88         ''' <see cref="System.Random.Next(Integer)"/>.(线程安全的函数)
89         ''' </summary>
90         ''' <param name="ceiling"></param>
91         ''' <returns></returns>
92         Public Function random(ceiling As IntegerAs Integer
93             SyncLock r
94                 Return r.[Next](ceiling)
95             End SyncLock
96         End Function
97
98         Public Sub randomize()
99             r = New Random(Now.Millisecond)
100         End Sub
101
102         Public Sub srand(seed__1 As Integer)
103             r = New Random(seed__1)
104         End Sub
105     End Module
106 End Namespace