zoukankan      html  css  js  c++  java
  • random between [a,b]、(a,b]、[a,b)

    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    const int n = 10;
    /*cstdlib头文件要和ctime一起,否则无法使用srand*/
    void RandBetween(int s, int d, int num)
    {
        int result;
        srand((unsigned)time(NULL));
        for(int i=0; i<num; i++)
        {
            result = rand() % (d - s) + s + 1;// (s,d]
            result = rand() % (d - s) + s;// [s,d)
            result = rand() % (d - s + 1) + s;// [s,d]
            cout << result << endl;
        }
    }
    
    int main()
    {
        int first, second;
        cout << "input start and end: ";
        cin >> first >> second;
        RandBetween(first, second, n);
        return 0;
    }
  • 相关阅读:
    Java语言
    包名规范
    带参数的方法
    成员变量和局部变量
    Java数据类型
    java反射机制
    JDK安装
    注释
    二维数组
    数组的经典排序
  • 原文地址:https://www.cnblogs.com/buptmuye/p/3667313.html
Copyright © 2011-2022 走看看