【问题标题】:Random Function Generator between two integers C++两个整数之间的随机函数生成器 C++
【发布时间】:2013-10-14 19:51:03
【问题描述】:

假设您有一个完全定义且正确的函数,其原型如下

int randomBetween( int lowerBound, int upperBound );

返回一个介于上下限之间的随机数。

编写一个 main 函数,使用 randomBetween 生成 1 到 10 之间的 1000 个值,包括 1 和 10。 随着值的生成,计算该值等于 5 的次数。 显示该值等于五的次数。 您的程序应该只输出一个数字(即,不要将 cout 语句放入循环中)。

这是我目前所拥有的:

   // Function Prototypes
   int randomBetween( int lowerBound, int upperBound );

   int main( )
{

  int lowerbound, upperbound;

  cout << "Enter the value for the lower bound: " ;
  cin >> lowerbound;

  cout << "Enter the value for the upper bound ( lower < upper ) : " ;
  cin >> upperbound;

  cout << "The random value between " << lowerbound << " and "<< upperbound 
       << " is " << randomBetween << endl;

  return EXIT_SUCCESS;
}

int randomBetween( int lowerBound, int upperBound )
{
  int upperbound, lowerbound;
  int randomBetween = rand() % (upperbound-lowerbound) + upperbound;
  return randomBetween;
}

当我编译程序并输入下限和上限的值时,我得到的答案是:00E9131B

【问题讨论】:

标签: c++ function random


【解决方案1】:

替换这一行

cout << "The random value between " << lowerbound << " and "<< upperbound 
   << " is " << randomBetween << endl;

有了这个

cout << "The random value between " << lowerbound << " and "<< upperbound 
   << " is " << randomBetween(lowerbound, upperbound) << endl;

你所做的是打印出函数地址。您需要使用参数调用函数。


也改一下

int randomBetween( int lowerBound, int upperBound )
{
  int upperbound, lowerbound;
  int randomBetween = rand() % (upperbound-lowerbound) + upperbound;
  return randomBetween;
}

int randomBetween( int lowerBound, int upperBound )
{
  int randomBetween = rand() % (upperbound-lowerbound) + upperbound;
  return randomBetween;
}

你不需要在函数中再次重新声明参数

【讨论】:

  • 当我这样做时,编译器会给出一个错误,指出变量下界和上界未初始化。
  • 感谢 xlc。如果我想使用 for 循环运行该函数 10 次怎么办?
  • 然后将 main 的主体放入 for 循环中
【解决方案2】:

这个:

cout << "The random value between " << lowerbound << " and "<< upperbound 
     << " is " << randomBetween << endl

应该是:

cout << "The random value between " << lowerbound << " and "<< upperbound 
     << " is " << randomBetween(lowerbound,uppderbound) << endl

在 randomBetween 函数中,您不需要声明变量上限和下限,因为它们已经传递给函数。

此外,将变量命名为与函数同名的做法被认为是不好的做法,因此您可能应该在 randomBetween 函数中重命名“randomBetween”变量。

您可能需要查看 rand() 函数的参考,因为您的 randomBetween 函数不会返回所需范围内的值:

http://en.cppreference.com/w/cpp/numeric/random/rand

我不确定您是否将它从您发布的内容中排除,但不要忘记包含“cstdlib”和“iostream”库:

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

【讨论】:

    【解决方案3】:

    首先,您的 C++ 代码包含一些错误。看起来您返回了函数的地址。即使 C++ 中的函数不带参数,在调用时,您始终必须在函数名后面加上括号:function()。在您的情况下,它确实需要正确传递给它的参数function(param0, param1, param2)
    您似乎在假设,因为您将变量命名为 lowerboundupperbound 与将隐式传递的函数参数相同,但这不是 C++ 的工作方式。
    但是,您可以将 lowerboundupperbound 定义为全局变量,并且您的函数 randomBetween() 不带任何参数。您可以通过在函数范围外部定义来设置全局变量。
    您还需要初始化所有变量。只需在定义 lowerboundupperbound 时将它们设置为 0

    【讨论】:

      【解决方案4】:

      您的函数不使用参数,

      int randomBetween( int lowerBound, int upperBound )
      {
          int randomBetween = rand() % (upperBound-lowerBound) + upperBound;
          return randomBetween;
      }
      

      并使用参数调用函数,

      ... << randomBetween(lowerbound, upperbound) <<endl;
      

      【讨论】:

      • 当我这样做时,编译器会给出一个错误,指出变量下界和上界未初始化。
      猜你喜欢
      • 2015-01-31
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2011-05-03
      • 2014-12-19
      相关资源
      最近更新 更多