⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rand.h

📁 这个一个随机数生成类,大学时写程序经常用到,如果不用类库中的,用我这个也可以的
💻 H
字号:
/**********************************************************************************
*                                                                                 *
*  Copyright (c) by Royal. All rights reserved.                                   *
*                                                                                 *
*  Permission to use, copy, modify, and distribute this software for any purpose  *
*  is hereby granted without fee, provided that this copyright and permissions    *
*  notice appear in all copies and derivatives, and that no charge may be made    *
*  for the software and its documentation except to cover cost of distribution.   *
*                                                                                 *
*  This software is provided "as is" without express or implied warranty.         *
*                                                                                 *
**********************************************************************************/

/*
*  Description:
*
*    A simple pseudorandom number generator class.
*
*  History:
*
*    Initial version created by Royal, July, 2004.
*
*  Notes:
*
*    This code has been written to conform to standard C++ and STL. It has been
*    compiled successfully using GNU C++ 3.2 (MinGW), Borland C++ 5.5, and Visual C++ 7.0.
*
*/
#ifndef RAND_H
#define RAND_H

#include <ctime>
#include <cstdlib>

class Rand
{
public:
    Rand()
    {
        std::srand((unsigned int)std::time(0));
        rand_ = std::rand();
    }
    
    Rand(int upper, int low = 0)
    {
        std::srand((unsigned int)std::time(NULL));
        int temp;
        while (1) 
        {
            temp = std::rand() % upper;
            if (temp >= low)
            {
                rand_ = temp;
                break;
            }
        }        
    }
    
    operator int() const
    { 
        return rand_; 
    }
private:
    int rand_;
};

#endif

⌨️ 快捷键说明

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