Pārlūkot izejas kodu

cmMath.h.c : Added cmRandXXX() functions.

master
kevin 10 gadus atpakaļ
vecāks
revīzija
83841f4934
2 mainītis faili ar 33 papildinājumiem un 2 dzēšanām
  1. 28
    0
      cmMath.c
  2. 5
    2
      cmMath.h

+ 28
- 0
cmMath.c Parādīt failu

@@ -391,3 +391,31 @@ double             cmFfSwapULLongToDouble( unsigned long long v )
391 391
   u.u = cmSwap64(v);
392 392
   return u.f;
393 393
 }
394
+
395
+int      cmRandInt( int min, int max )
396
+{
397
+  assert( min <= max );
398
+  int offs = max - min;
399
+  return min + cmMax(0,cmMin(offs,(int)round(offs * (double)rand() / RAND_MAX)));
400
+}
401
+
402
+unsigned cmRandUInt( unsigned min, unsigned max )
403
+{
404
+  assert( min <= max );
405
+  unsigned offs = max - min;
406
+  return min + cmMax(0,cmMin(offs,(unsigned)round(offs * (double)rand() / RAND_MAX)));
407
+}
408
+
409
+float    cmRandFloat( float min, float max )
410
+{
411
+  assert( min <= max );
412
+  float offs = max - min;
413
+  return min + cmMax(0,cmMin(offs,(float)(offs * (double)rand() / RAND_MAX)));
414
+}
415
+
416
+double   cmRandDouble( double min, double max )
417
+{
418
+  assert( min <= max );
419
+  double offs = max - min;
420
+  return min + cmMax(0,cmMin(offs,(offs * (double)rand() / RAND_MAX)));
421
+}

+ 5
- 2
cmMath.h Parādīt failu

@@ -62,6 +62,9 @@ float              cmFfSwapUIntToFloat( unsigned v );
62 62
 unsigned long long cmFfSwapDoubleToULLong( double v );
63 63
 double             cmFfSwapULLongToDouble( unsigned long long v );
64 64
 
65
-
66
-
65
+//=================================================================
66
+int      cmRandInt( int min, int max );
67
+unsigned cmRandUInt( unsigned min, unsigned max );
68
+float    cmRandFloat( float min, float max );
69
+double   cmRandDouble( double min, double max );
67 70
 #endif

Notiek ielāde…
Atcelt
Saglabāt