传递参数,参数,交换函数和随机数生成器

时间:2017-02-24 05:09:04

标签: c++

我正在尝试创建一个随机数生成器,首先交换最小值和最大值,然后再次交换它们(回到它们的原始状态?)。我的另外两个问题是:如何判断交换是否实际发生(因为它似乎是一个不可见的过程),无论选择哪两个值作为最小值和最大值,随机数输出始终为1。

#include <string>
#include <cstdlib>
#include <iostream>
using namespace std;

int minimum;
int maximum;

//Swap function
void swap (int& minimum, int& maximum) {
    minimum = maximum;
    maximum = minimum;
}


int randomNumber;
//Random number generator
void randomNum() {
    minimum = minimum;
    maximum = maximum;
    srand(time(NULL));
    randomNumber = rand() % maximum + minimum;

    cout<<"Your random value is: " << randomNum;

}


int main() {

    cout<<"Please enter your minimum value: " << endl;
    cin>>minimum;

    cout<<"Please enter your maximum value: " << endl;
    cin>>maximum;

     swap (minimum, maximum); //Calling swap function
     randomNum(); //Needs to swap again somehow.

}

0 个答案:

没有答案