소스 검색

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

master
kevin 10 년 전
부모
커밋
83841f4934
2개의 변경된 파일33개의 추가작업 그리고 2개의 파일을 삭제
  1. 28
    0
      cmMath.c
  2. 5
    2
      cmMath.h

+ 28
- 0
cmMath.c 파일 보기

@@ -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 파일 보기

@@ -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

Loading…
취소
저장