How would I use a function to return a random number?

Pages: 123
I just learned how to make random numbers with the code below. I also just learned about functions. How would I use a function to return a random number between 11 and 23, inclusive. I have the random part down, I just am not sure how to return a function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

int randomNumber(int val1, int val2) // Function prototype

int main ()
{
    int iseed = (int)time(NULL);
    int randomNumber;
    srand(iseed);

    randomNumber = rand()%(23 - 11 + 1) + 11; // random number from 11 to 23 inclusive
    cout << randomNumber;

return 0;
}

                                              // Function definition 
Hi,

Try this :
1
2
3
4
5
6
7
int randomNumber(int _val1, int _val2)
 {
     int val1 = std::min(_val1, _val2);
     int val2 = std::max(_val1, _val2);

     return ((rand() % (val2 - val1)) + val1);
 }
And your function main() :
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    int iseed = (int)time(NULL);
   srand(iseed);

    int myNumber = randomNumber(11, 23);

    cout << "randomNumber == " << myNumber;

    return 0;
}
Last edited on
Does that help you? :)
Please read the FAQ. (Ignore the stuff that says "platform specific".)
http://www.cplusplus.com/faq/beginners/random-numbers/

    1. Seed in main()
    2. Use random_int_in_range() to get a random number.

Hope this helps.
I still cannot get any of yours too run
I want to show the returned value in the main().
Cut-n-paste, dude.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstdlib>
#include <ctime>

int random_int_in_range( int first, int last )
{
  /* This function implements the method recommended by the knowing
   * folks at comp.lang.c: http://c-faq.com/lib/randrange.html
   * Returns an integer in [first, last].
   */
  unsigned int N = (last - first <= RAND_MAX)  /* Make sure the algorithm    */
                 ? (last - first + 1U)         /* terminates by keeping N    */
                 : (RAND_MAX + 1U);            /* in rand()'s maximum range. */
  unsigned int x = (RAND_MAX + 1U) / N;
  unsigned int y = x * N;
  unsigned int r;
  do {
    r = rand();
  } while (r >= y);
  return r / x + first;
}

#include <iomanip>
#include <iostream>
#include <map>
using namespace std; 

int main()
{
  srand( time( NULL ) );
  

  // Let's pull 100 integers in [11, 23] from the PRNG and 
  // see how many of each integer we get.
  map <int, int> histogram;
  
  for (int n = 0; n < 100; n++)
    histogram[ random_int_in_range( 11, 23 ) ]++;
  
  for (auto p : histogram)
    cout << p.first << " : " << setw( 2 ) << p.second << " times\n";
}

Hope this helps.
> I want to show the returned value in the main()?
Do you mind telling us what you meant by that?
@ Duoas: The OP has just learned about functions recently, and you are giving him a code with maps? Srs?
we haven't learned maps yet. I am looking for something more simple perhaps
So can you tell exactly what is wrong with my solution?
> How would I use a function to return a random number?
This is how you use a function to return a random number :

int myRandomNum = randomNumber(11, 23);
.
cout << randomNumber(11, 23);
I think closed accounts solution is good for a beginner like OP. Just a slight modification though:

1
2
3
4
5
6
7
int randomNumber(int _val1, int _val2)
 {
     int val1 = std::min(_val1, _val2);
     int val2 = std::max(_val1, _val2);

     return ((rand() % (val2 - val1)) + val1 + 1);
 }


@closed account: You solution would give 11-22, NOT 11-23 as the OP wants.
Also, I only need 1 random number. Not sure why you did so much extra work
I think Arslan7041's solution is good for a beginner like OP. Just a slight modification though :

1
2
3
4
5
6
7
int randomNumber(int _val1, int _val2)
 {
     int val1 = std::min(_val1, _val2);
     int val2 = std::max(_val1, _val2);

     return ((rand() % (val2 - val1 + 1)) + val1);
 }


@Arslan7041: You solution would give 12-23, NOT 11-23 as the OP wants.
@Closed account: Thank you for pointing that out. Revised solution:

1
2
3
4
5
6
7
int randomNumber(int _val1, int _val2)
 {
     int val1 = std::min(_val1, _val2);
     int val2 = std::max(_val1, _val2);

     return ((rand() % ( val2 - val1 + 1 ) + ( val1) );
 }
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <ctime>

int randomNumber(int, int);

int main ()
{
    srand((int)time(NULL));
    std::cout << randomNumber(11, 23) << '\n';
    
    return 0;
}

int randomNumber(int val1, int val2)
{
    return rand()%(val2 - val1 + 1) + val1;
} 
Last edited on
Thank you! That looks more familiar

@ Kemort
closed account (48T7M4Gy)
My pleasure Austinomical.

FWIW, all I did was copy your line 14 and 'converted' it in the function to the general form and then returned it. You did all the work :)

Last edited on
Pages: 123