Jypeli  5
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.
28  */
29 
30 using System;
31 using AdvanceMath;
32 using System.Collections.Generic;
33 
34 namespace Jypeli
35 {
39  public static class RandomGen
40  {
41  private static Random rand = new Random();
42 
47  public static bool NextBool()
48  {
49  return ( rand.NextDouble() >= 0.5 );
50  }
51 
56  public static int NextInt( int maxValue )
57  {
58  return rand.Next( maxValue );
59  }
60 
67  public static int NextInt( int min, int max )
68  {
69  return rand.Next( min, max );
70  }
71 
78  public static double NextDouble(double min, double max)
79  {
80  return min + rand.NextDouble() * ( max - min );
81  }
82 
88  public static char NextLetter(bool upperCase = false)
89  {
90  int startCode = upperCase ? (int)'A' : (int)'a';
91  int endCode = upperCase ? (int)'Z' : (int)'z';
92  return (char)NextInt( startCode, endCode + 1 );
93  }
94 
99  public static Direction NextDirection()
100  {
101  double randdir = rand.NextDouble();
102 
103  if ( randdir <= 0.25 )
104  return Direction.Up;
105 
106  if ( randdir <= 0.5 )
107  return Direction.Down;
108 
109  if ( randdir <= 0.75 )
110  return Direction.Left;
111 
112  return Direction.Right;
113  }
114 
122  public static double[] NextDoubleArray( double min, double max, int size )
123  {
124  double[] array = new double[size];
125 
126  for ( int i = 0; i < size; i++ )
127  {
128  array[i] = NextDouble( min, max );
129  }
130 
131  return array;
132  }
133 
142  public static double[] NextDoubleArray( double min, double max, int size, int maxchange )
143  {
144  double[] array = new double[size];
145  double curmin = min;
146  double curmax = max;
147 
148  for ( int i = 0; i < size; i++ )
149  {
150  array[i] = NextDouble( curmin, curmax );
151 
152  curmin = AdvanceMath.MathHelper.Max((float)min, (float)(array[i] - maxchange));
153  curmax = AdvanceMath.MathHelper.Min((float)max, (float)(array[i] + maxchange));
154  }
155 
156  return array;
157  }
158 
163  public static Color NextColor()
164  {
165  return new Color( (float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble(), 1 );
166  }
167 
172  public static Color NextLightColor()
173  {
174  return Color.Mix( NextColor(), Color.White );
175  }
176 
181  public static Color NextDarkColor()
182  {
183  return Color.Mix( NextColor(), Color.Black );
184  }
185 
190  public static Color NextColor( Color first, Color second )
191  {
192  return Color.Lerp( first, second, (float)rand.NextDouble());
193  }
194 
199  public static Angle NextAngle()
200  {
201  return Angle.FromRadians( NextDouble( 0, 2 * Math.PI ) );
202  }
203 
210  public static Vector NextVector( double minLength, double maxLength )
211  {
212  return Vector.FromLengthAndAngle( NextDouble( minLength, maxLength ), NextAngle() );
213  }
214 
223  public static Vector NextVector( double minX, double minY, double maxX, double maxY )
224  {
225  return new Vector( NextDouble( minX, maxX ), NextDouble( minY, maxY ) );
226  }
227 
228 
235  public static Vector NextVector(BoundingRectangle rect, int r=0)
236  {
237  return new Vector(NextDouble(rect.Left+r, rect.Right-r), NextDouble(rect.Bottom+r, rect.Top-r));
238  }
239 
240 
246  public static Angle NextAngle( Angle max )
247  {
248  return Angle.FromRadians( NextDouble( 0, max.Radians ) );
249  }
250 
257  public static Angle NextAngle( Angle min, Angle max )
258  {
259  double a1 = min.Radians;
260  double a2 = max.Radians;
261  while ( a2 < a1 ) a2 += 2 * Math.PI;
262  return Angle.FromRadians( NextDouble( a1, a2 ) );
263  }
264 
276  public static int NextIntWithProbabilities( params double[] p )
277  {
278  double randomNum = rand.NextDouble();
279  double accumulator = 0;
280 
281  for ( int i = 0; i < p.Length; i++ )
282  {
283  accumulator += p[i];
284  if ( randomNum < accumulator ) return i;
285  }
286 
287  return p.Length;
288  }
289 
290 
296  public static void Shuffle<T>(IList<T> list)
297  {
298  for (int i = list.Count; i > 1; i--)
299  {
300  // Pick random element to swap.
301  int j = rand.Next(i); // 0 <= j <= i-1
302  // Swap.
303  T tmp = list[j];
304  list[j] = list[i - 1];
305  list[i - 1] = tmp;
306  }
307  }
308 
316  public static T SelectOne<T>( params T[] choices )
317  {
318  int i = NextInt( choices.Length );
319  return choices[i];
320  }
321 
329  public static T SelectOne<T>( IList<T> choices )
330  {
331  int i = NextInt( choices.Count );
332  return choices[i];
333  }
334 
335  }
336 }
static Direction Down
Suunta alas.
Definition: Direction.cs:65
static Color Mix(params Color[] colors)
Sekoittaa kahta tai useampaa väriä.
Definition: Color.cs:443
static Vector NextVector(BoundingRectangle rect, int r=0)
Luodaan satunnainen vektori, jonka "piste" on suorakaiteen sisällä.
Definition: RandomGen.cs:235
static Color NextLightColor()
Palauttaa satunnaisen vaalean värin.
Definition: RandomGen.cs:172
static Direction NextDirection()
Palauttaa satunnaisen suunnan.
Definition: RandomGen.cs:99
static readonly Color Black
Musta.
Definition: Color.cs:494
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40
static Direction Left
Suunta vasemmalle.
Definition: Direction.cs:70
static bool NextBool()
Palauttaa satunnaisen totuusarvon.
Definition: RandomGen.cs:47
double Bottom
Suorakaiteen alareunen Y
Satunnaisgeneraattori. Luo satunnaisia arvoja, mm. lukuja, vektoreita sekä kulmia.
Definition: RandomGen.cs:39
static char NextLetter(bool upperCase=false)
Arpoo satunnaisen kirjaimen väliltä a-z.
Definition: RandomGen.cs:88
static void Shuffle< T >(IList< T > list)
Sotkee rakenteen satunnaiseen järjestykseen
Definition: RandomGen.cs:296
double Right
Suorakaiteen oikean reunan X
static double [] NextDoubleArray(double min, double max, int size, int maxchange)
Palauttaa double-taulukon täytettyinä satunnaisilla luvuilla väliltä [min,max]
Definition: RandomGen.cs:142
static double NextDouble(double min, double max)
Palauttaa satunnaisen liukuluvun parametrien
Definition: RandomGen.cs:78
static Vector NextVector(double minX, double minY, double maxX, double maxY)
Palauttaa satunnaisen vektorin.
Definition: RandomGen.cs:223
static Color Lerp(Color value1, Color value2, double amount)
Definition: Color.cs:392
static int NextInt(int maxValue)
Palauttaa satunnaisen kokonaisluvun, joka on vähintään 0 ja pienempi kuin
Definition: RandomGen.cs:56
static int NextIntWithProbabilities(params double[] p)
Palauttaa satunnaisen kokonaisluvun annettujen todennäköisyyksien perusteella.
Definition: RandomGen.cs:276
static Color NextDarkColor()
Palauttaa satunnaisen tumman värin.
Definition: RandomGen.cs:181
static Angle NextAngle(Angle max)
Palauttaa satunnaisen kulman nollasta annettuun maksimiin.
Definition: RandomGen.cs:246
Perussuunta tasossa.
Definition: Direction.cs:50
double Left
Suorakaiteen vasemman reunan X
static T SelectOne< T >(params T[] choices)
Palauttaa yhden annetuista vaihtoehdoista. Esim. RandomGen.SelectOne<string>("yksi", "kaksi", "kolme");
Definition: RandomGen.cs:316
static Angle FromRadians(double radian)
Luo kulman annettujen radiaanien mukaan.
Definition: Angle.cs:316
double Top
Suorakaiteen yläreunan Y
static double [] NextDoubleArray(double min, double max, int size)
Palauttaa double-taulukon täytettyinä satunnaisilla luvuilla väliltä [min,max]
Definition: RandomGen.cs:122
double Radians
Palauttaa tai asettaa kulman radiaaneina.
Definition: Angle.cs:86
static Color NextColor()
Palauttaa satunnaisen värin.
Definition: RandomGen.cs:163
Väri.
Definition: Color.cs:13
static Angle NextAngle()
Palauttaa satunnaisen kulman.
Definition: RandomGen.cs:199
static Angle NextAngle(Angle min, Angle max)
Palauttaa satunnaisen kulman tietyltä väliltä.
Definition: RandomGen.cs:257
static Vector NextVector(double minLength, double maxLength)
Palauttaa satunnaisen vektorin.
Definition: RandomGen.cs:210
static int NextInt(int min, int max)
Palauttaa satunnaisen kokonaisluvun, joka on vähintään
Definition: RandomGen.cs:67
static Direction Right
Suunta oikealle.
Definition: Direction.cs:75
static Direction Up
Suunta ylös.
Definition: Direction.cs:60
2D-vektori.
Definition: Vector.cs:56
static Color NextColor(Color first, Color second)
Palauttaa satunnaisen värin.
Definition: RandomGen.cs:190
static readonly Color White
Valkoinen.
Definition: Color.cs:894