Jypeli 10
The simple game programming library
RandomGen.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2009 University of Jyväskylä, Department of Mathematical
4 * Information Technology.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#endregion
25
26/*
27 * Authors: Tero Jäntti, Tomi Karppinen, Janne Nikkanen, Rami Pasanen.
28 */
29
30using System;
31using System.Collections.Generic;
32
33namespace Jypeli
34{
38 public static class RandomGen
39 {
40 private static Random rand = new Random();
41
45 public static bool NextBool()
46 {
47 return ( rand.NextDouble() >= 0.5 );
48 }
49
54 public static int NextInt( int maxValue )
55 {
56 return rand.Next( maxValue );
57 }
58
62 public static int NextInt( int min, int max )
63 {
64 return rand.Next( min, max );
65 }
66
70 public static double NextDouble( double min, double max )
71 {
72 return min + rand.NextDouble() * ( max - min );
73 }
74
80 public static char NextLetter(bool upperCase = false)
81 {
82 int startCode = upperCase ? (int)'A' : (int)'a';
83 int endCode = upperCase ? (int)'Z' : (int)'z';
84 return (char)NextInt( startCode, endCode + 1 );
85 }
86
90 public static Direction NextDirection()
91 {
92 double randdir = rand.NextDouble();
93
94 if ( randdir <= 0.25 )
95 return Direction.Up;
96
97 if ( randdir <= 0.5 )
98 return Direction.Down;
99
100 if ( randdir <= 0.75 )
101 return Direction.Left;
102
103 return Direction.Right;
104 }
105
113 public static double[] NextDoubleArray( double min, double max, int size )
114 {
115 double[] array = new double[size];
116
117 for ( int i = 0; i < size; i++ )
118 {
119 array[i] = NextDouble( min, max );
120 }
121
122 return array;
123 }
124
133 public static double[] NextDoubleArray( double min, double max, int size, int maxchange )
134 {
135 double[] array = new double[size];
136 double curmin = min;
137 double curmax = max;
138
139 for ( int i = 0; i < size; i++ )
140 {
141 array[i] = NextDouble( curmin, curmax );
142
143 curmin = AdvanceMath.MathHelper.Max((float)min, (float)(array[i] - maxchange));
144 curmax = AdvanceMath.MathHelper.Min((float)max, (float)(array[i] + maxchange));
145 }
146
147 return array;
148 }
149
154 public static Color NextColor()
155 {
156 return new Color( (float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble(), 1 );
157 }
158
163 public static Color NextLightColor()
164 {
165 return Color.Mix( NextColor(), Color.White );
166 }
167
172 public static Color NextDarkColor()
173 {
174 return Color.Mix( NextColor(), Color.Black );
175 }
176
183 public static Color NextColor( object obj )
184 {
185 var r = new Random( obj.GetHashCode() );
186 return new Color( (float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble(), 1 );
187 }
188
193 public static Color NextColor( Color first, Color second )
194 {
195 return Color.Lerp( first, second, (float)rand.NextDouble());
196 }
197
202 public static Angle NextAngle()
203 {
204 return Angle.FromRadians( NextDouble( 0, 2 * Math.PI ) );
205 }
206
213 public static Vector NextVector( double minLength, double maxLength )
214 {
215 return Vector.FromLengthAndAngle( NextDouble( minLength, maxLength ), NextAngle() );
216 }
217
226 public static Vector NextVector( double minX, double minY, double maxX, double maxY )
227 {
228 return new Vector( NextDouble( minX, maxX ), NextDouble( minY, maxY ) );
229 }
230
237 public static Vector NextVector( BoundingRectangle rect, int r = 0 )
238 {
239 return new Vector( NextDouble( rect.Left + r, rect.Right - r ), NextDouble( rect.Bottom + r, rect.Top - r ) );
240 }
241
247 public static Angle NextAngle( Angle max )
248 {
249 return Angle.FromRadians( NextDouble( 0, max.Radians ) );
250 }
251
258 public static Angle NextAngle( Angle min, Angle max )
259 {
260 double a1 = min.Radians;
261 double a2 = max.Radians;
262 while ( a2 < a1 ) a2 += 2 * Math.PI;
263 return Angle.FromRadians( NextDouble( a1, a2 ) );
264 }
265
272 public static TimeSpan NextTimeSpan( double minSeconds, double maxSeconds )
273 {
274 return TimeSpan.FromSeconds( NextDouble( minSeconds, maxSeconds ) );
275 }
276
280 public static Shape NextShape()
281 {
282 return SelectOne(new Shape[] { Shape.Diamond, Shape.Ellipse,
285 }
286
298 public static int NextIntWithProbabilities( params double[] p )
299 {
300 double randomNum = rand.NextDouble();
301 double accumulator = 0;
302
303 for ( int i = 0; i < p.Length; i++ )
304 {
305 accumulator += p[i];
306 if ( randomNum < accumulator ) return i;
307 }
308
309 return p.Length;
310 }
311
312
318 public static void Shuffle<T>(IList<T> list)
319 {
320 for (int i = list.Count; i > 1; i--)
321 {
322 // Pick random element to swap.
323 int j = rand.Next(i); // 0 <= j <= i-1
324 // Swap.
325 T tmp = list[j];
326 list[j] = list[i - 1];
327 list[i - 1] = tmp;
328 }
329 }
330
338 public static T SelectOne<T>( params T[] choices )
339 {
340 int i = NextInt( choices.Length );
341 return choices[i];
342 }
343
351 public static T SelectOne<T>( IList<T> choices )
352 {
353 int i = NextInt( choices.Count );
354 return choices[i];
355 }
356 }
357}
System.Drawing.Color Color
static Scalar Max(params Scalar[] vals)
Definition: MathHelper.cs:228
static Scalar Min(params Scalar[] vals)
Definition: MathHelper.cs:242
Satunnaisgeneraattori. Luo satunnaisia arvoja, mm. lukuja, vektoreita sekä kulmia.
Definition: RandomGen.cs:39
static void Shuffle< T >(IList< T > list)
Sotkee rakenteen satunnaiseen järjestykseen
Definition: RandomGen.cs:318
static Vector NextVector(BoundingRectangle rect, int r=0)
Luodaan satunnainen vektori, jonka "piste" on suorakaiteen sisällä.
Definition: RandomGen.cs:237
static Angle NextAngle(Angle min, Angle max)
Palauttaa satunnaisen kulman tietyltä väliltä.
Definition: RandomGen.cs:258
static double[] NextDoubleArray(double min, double max, int size, int maxchange)
Palauttaa double-taulukon.
Definition: RandomGen.cs:133
static char NextLetter(bool upperCase=false)
Arpoo satunnaisen kirjaimen väliltä a-z.
Definition: RandomGen.cs:80
static int NextInt(int min, int max)
Palauttaa satunnaisen kokonaisluvun, joka on vähintään
Definition: RandomGen.cs:62
static Random rand
Definition: RandomGen.cs:40
static Angle NextAngle()
Palauttaa satunnaisen kulman.
Definition: RandomGen.cs:202
static Color NextColor(Color first, Color second)
Palauttaa satunnaisen värin.
Definition: RandomGen.cs:193
static Color NextDarkColor()
Palauttaa satunnaisen tumman värin.
Definition: RandomGen.cs:172
static Color NextLightColor()
Palauttaa satunnaisen vaalean värin.
Definition: RandomGen.cs:163
static Vector NextVector(double minLength, double maxLength)
Palauttaa satunnaisen vektorin.
Definition: RandomGen.cs:213
static int NextIntWithProbabilities(params double[] p)
Palauttaa satunnaisen kokonaisluvun annettujen todennäköisyyksien perusteella.
Definition: RandomGen.cs:298
static Shape NextShape()
Palauttaa satunnaisen muodon.
Definition: RandomGen.cs:280
static double NextDouble(double min, double max)
Palauttaa satunnaisen liukuluvun parametrien
Definition: RandomGen.cs:70
static T SelectOne< T >(params T[] choices)
Palauttaa yhden annetuista vaihtoehdoista. Esim. RandomGen.SelectOne<string>("yksi",...
Definition: RandomGen.cs:338
static Color NextColor()
Palauttaa satunnaisen värin.
Definition: RandomGen.cs:154
static bool NextBool()
Palauttaa satunnaisen totuusarvon.
Definition: RandomGen.cs:45
static double[] NextDoubleArray(double min, double max, int size)
Palauttaa double-taulukon.
Definition: RandomGen.cs:113
static Vector NextVector(double minX, double minY, double maxX, double maxY)
Palauttaa satunnaisen vektorin.
Definition: RandomGen.cs:226
static Direction NextDirection()
Palauttaa satunnaisen suunnan.
Definition: RandomGen.cs:90
static Angle NextAngle(Angle max)
Palauttaa satunnaisen kulman nollasta annettuun maksimiin.
Definition: RandomGen.cs:247
static Color NextColor(object obj)
Palauttaa satunnaisen värin olioilmentymän perusteella. Sama olio palauttaa aina saman värin.
Definition: RandomGen.cs:183
static TimeSpan NextTimeSpan(double minSeconds, double maxSeconds)
Palauttaa satunnaisen aikavälin.
Definition: RandomGen.cs:272
static int NextInt(int maxValue)
Palauttaa satunnaisen kokonaisluvun, joka on vähintään 0 ja pienempi kuin
Definition: RandomGen.cs:54
Kuvio.
Definition: Shapes.cs:47
static readonly Rectangle Rectangle
Suorakulmio.
Definition: Shapes.cs:73
static readonly Triangle Triangle
Tasasivuinen kolmio.
Definition: Shapes.cs:78
static readonly Ellipse Ellipse
Ellipsi tai ympyrä.
Definition: Shapes.cs:68
static readonly Shape Octagon
Oktagoni eli kahdeksankulmio.
Definition: Shapes.cs:108
static readonly Star Star
Tähti.
Definition: Shapes.cs:88
static readonly Shape Diamond
Timantti- / salmiakkikuvio
Definition: Shapes.cs:93
static readonly Shape Hexagon
Heksagoni eli kuusikulmio.
Definition: Shapes.cs:103
static readonly Heart Heart
Sydän.
Definition: Shapes.cs:83
static readonly Shape Pentagon
Pentagoni eli viisikulmio.
Definition: Shapes.cs:98
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40
static Angle FromRadians(double radian)
Luo kulman annettujen radiaanien mukaan.
Definition: Angle.cs:315
double Radians
Palauttaa tai asettaa kulman radiaaneina.
Definition: Angle.cs:85
double Top
Suorakaiteen yläreunan Y
double Right
Suorakaiteen oikean reunan X
double Left
Suorakaiteen vasemman reunan X
double Bottom
Suorakaiteen alareunen Y
Väri.
Definition: Color.cs:13
static readonly Color White
Valkoinen.
Definition: Color.cs:956
static readonly Color Black
Musta.
Definition: Color.cs:556
static Color Mix(params Color[] colors)
Sekoittaa kahta tai useampaa väriä.
Definition: Color.cs:505
static Color Lerp(Color value1, Color value2, double amount)
Lineaarinen interpolaatio värien välillä
Definition: Color.cs:455
Perussuunta tasossa.
Definition: Direction.cs:47
static Direction Up
Suunta ylös.
Definition: Direction.cs:56
static Direction Right
Suunta oikealle.
Definition: Direction.cs:71
static Direction Down
Suunta alas.
Definition: Direction.cs:61
static Direction Left
Suunta vasemmalle.
Definition: Direction.cs:66
2D-vektori.
Definition: Vector.cs:67
static Vector FromLengthAndAngle(double length, double angle)
Luo vektorin pituuden ja kulman perusteella.
Definition: Vector.cs:114