random.cpp

来自「通用网络游戏开发框架」· C++ 代码 · 共 49 行

CPP
49
字号
// ============================================================================
// Random
//
// Return a random number within the specified range
//
// (c) 2003 Ken Reed
//
// This is free software. You can redistribute it and/or modify it under the
// terms of the GNU General Public License version 2 as published by the Free
// Software Foundation.
// ============================================================================

#include "stdafx.h"

#include <stdlib.h>
#include <time.h>


// ============================================================================
// Return double value
// ============================================================================

double random(double minimum, double maximum)
{
   static bool first_time = true;

   if (first_time) {
      srand((int) time(0));
      first_time = false;
   }

   double range = maximum - minimum;
   double rval  = (double(rand())/RAND_MAX) * range;

   rval += minimum;

   return rval;
}


// ============================================================================
// Return int value
// ============================================================================

int irandom(int minimum, int maximum) 
{
   return static_cast<int>(random(minimum, maximum));
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?