Generating random numbers / writing the main function

Hello everyone,
so i am basically new to C++ coding and i am trying to write the randNum function in a way that it will give me these ranges

The range for this row is: 1 to 10: 5 4 10 2 7 9 1 8 9 6
The range for this row is: 11 to 20: 20 15 16 11 20 11 13 13 13 11
The range for this row is: 21 to 30: 28 28 25 30 30 25 29 28 29 28
The range for this row is: 31 to 40: 39 33 34 40 37 40 31 37 37 31
The range for this row is: 41 to 50: 44 48 45 50 50 47 42 44 49 46
The range for this row is: 51 to 60: 55 59 55 51 60 56 57 60 53 56
The range for this row is: 61 to 70: 67 63 68 70 65 64 61 65 62 70
The range for this row is: 71 to 80: 77 76 79 72 77 79 80 78 72 78
The range for this row is: 81 to 90: 85 86 88 90 89 87 85 85 86 90
The range for this row is: 91 to 100: 100 95 92 98 94 96 93 95 92 95

this is what i've written so far and i got stuck, any help would be really appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main() {
    srand(time(0));   // to get new random numbers for each run
    int first, last;
    first = 0;
    last = 9;
    for (int r = 1; r <= 100; r+=10) {
        cout<<"The range for this row is: "<<first+r<<" to "<<last+r<<": ";
        for(int c = 1; c <= 10; c++) {
            cout<<randNum(first+r,last+r)<<" ";
        }
        cout<<endl;
    }
    cout << endl;
    return 0;
}  
Last edited on
right but how would you go by writing it the way i was asked ?
You could make a function that does all the work, and just pass it the minimum and maximum values.
1
2
3
4
int random(int low,int high)
{
    return rand()%(high-low+1)+low;
}

This way, you can just add ten to the minimum and maximum values for each iteration of the loop, the same way you did your output for the range of each row. Maybe something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include<iostream>//required for cout
#include<windows.h>//required for rand
#include<time.h>//required for time
using namespace std;

int random(int low,int high)
{
    return rand()%(high-low+1)+low;
}

int main()
{
    srand(time(NULL));//set random # seed
    for (int r=1;r<101;r+=10)
    {
     cout<<"The range for this row is: "<<r<<" to "<<r+9<<": ";
     for (int c=r;c<r+10;c++)
           cout<<random(r,r+9)<<" ";
     cout<<endl;
    }
     return 0;
}
Last edited on
right, thank you cplusplus

but this is an assignment and i have to do it in a specific way.

would you be able to help me figure out how i would do it through that original method i have posted ?

1+(rand() % 10) would be for a range of 1-10

%10 Takes 10 numbers from rand and divides them by 10, then gives you the remainder which could either be 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.

Since it starts at 0 instead of 1 and since you can't get 10 as a remainder you need to add +1 to it.

I hope you understand now, I'm not that good at explaining stuff though so if you don't get it just watch the video. He explains it very well.

http://www.youtube.com/watch?v=naXUIEAIt4U
It looks to me from your code that you are using a function similar to my random function on line 13, randNum, which takes the minimum and maximum range of random values into it. What you need to do is create a function that behaves as in my example above if you wish to accomplish it this way. If you have flexibility in the assignment, you could also use
cout<<rand() % (last-first+1) + first; instead of randNum, although you need to add the value of r to both first and last.

You can think of rand() like this, whether using it directly or in a function:
//(# of possible values)+lowest value
cout<<rand()% 10 + 1 which results in output of 1-10
Last edited on
so i have this so far,

how would you go by editing it
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int randNum=rand() %10+r;


int main() {
    
    srand(time(0));   // to get new random numbers for each run
    
    int first, last;
    first = 0;
    last = 9;
    for (int r = 1; r <= 100; r+=10) {
        cout<<"The range for this row is: "<<first+r<<" to "<<last+r<<": ";
        for(int c = 1; c <= 10; c++) {
            cout<<randNum(first+r,last+r)<<" ";
        }
        cout<<endl;
    }
    cout << endl;
    
    return 0;
    
}
Good attempt at a function like I was referring to, but it won't work this way because it doesn't know what r is until afterwards. Instead, you tell it the type of parameters it's working with, and then supply the parameters you want later. Also, I made a mistake by oversimplifying my explanation earlier. The reason it doesn't work properly is because rand() returns a number between 0 and the range of values, but never including the whole range itself. By a little mathematical trickery, however, we can force it to spit out values between x and y while including those values in the range of it's output. By subtracting the lowest from the highest and adding 1, we get the correct number of possible values, and adding the lowest value desired back to that total results in the correct range no matter what values are involved.

So it should be more like this:
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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int randNum (int first,int last)
{
    return rand() %(last-first+1)+first;
}

int main() 
{
    srand(time(0));   // to get new random numbers for each run

    int first, last;
    first = 0;
    last = 9;
    for (int r = 1; r <= 100; r+=10)
        {
        cout<<"The range for this row is: "<<first+r<<" to "<<last+r<<": ";

        for(int c = 1; c <= 10; c++)
            cout<<randNum(first+r,last+r)<<" ";

        cout<<endl;
        }
    cout << endl;
    return 0;
}


This should give you the output you want without altering main's primary code (I only altered the formatting slightly). BTW, the function at the beginning does not have to use the same variable names, it just needs to know the variable types. Using different names in the function part, such as using x and y would also work. The values are supplied when the function is called upon in your output, and then it returns the value it gets based on the math in the function before outputting.

EDIT: Also, the formula rand()%10+r you used would result in the correct values, but you were already supplying two parameters in your code, so it was easier and more practical to use two parameters in the function like my earlier example to avoid straying outside of your assignment boundaries by altering your code further, not to mention the code is more reusable this way. Anytime in the future that you make a program involving random numbers, you can keep the function and srand commands there while replacing the rest of the code with your new program, and call on randNum(lowest,highest) to get the range of values desired any time you need to. Similarly, you can create other functions that do other things you prefer not to muck with the details of to handle various tasks. Once you have them working properly, they can in turn be transplanted into new programs as needed. In this respect, the fact that the variable names don't have to be the same is an advantage.
Last edited on
Topic archived. No new replies allowed.