int main() { int temp = 0; const int row = 10; const int col = 5; int arr[row][col]; srand(time(NULL)); temp = (rand() % 99 )+1; for (int i = 0; i<row; i++) { for (int j = 0; j<col; j++) { arr[i][j] = temp; cout <<setw(4)<< arr[i][j] << setw(5) << " | " ; } cout << endl; } return 0; }

# Help with random number generator

Page 1 of 1## 9 Replies - 807 Views - Last Post: 15 November 2017 - 06:48 AM

### #1

# Help with random number generator

Posted 08 November 2017 - 05:25 PM

Every time I run my program it's running the same numbers in my array, how would I make it so that it generates different numbers (1-99) in each column?

##
**Replies To:** Help with random number generator

### #2

## Re: Help with random number generator

Posted 08 November 2017 - 05:38 PM

You might try setting temp to a new value with each iteration of the loop. That won't guarantee each number is unique, but they won't all be the same either!

### #3

## Re: Help with random number generator

Posted 08 November 2017 - 08:29 PM

I'll try that! Thank you!

This post has been edited by **Skydiver**: 09 November 2017 - 06:39 AM

Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

### #4

## Re: Help with random number generator

Posted 09 November 2017 - 10:09 AM

This would probably be the preferred approach in modern C++ (i.e., C++11 and above):

More info about the <random> header can be found here.

#include <iomanip> #include <iostream> #include <random> using namespace std; int main() { const int row = 10; const int col = 5; int arr[row][col]; random_device rnd; mt19937 gen(rnd()); uniform_int_distribution<> dis(1, 99); for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { arr[i][j] = dis(gen); cout << setw(4) << arr[i][j] << setw(5) << " | "; } cout << endl; } return 0; }

More info about the <random> header can be found here.

This post has been edited by **GazinAtCode**: 09 November 2017 - 10:10 AM

### #5

## Re: Help with random number generator

Posted 13 November 2017 - 01:35 PM

How would I multiply the elements in the rows and columns? For example the first row is multiplied by the second row, the second is multiplied by the third, the fourth by the fifth, and the fifth by the first row. Then, I would display it in another array.

### #6

## Re: Help with random number generator

Posted 14 November 2017 - 12:30 AM

You mean something like

*row*and then_{i}* row_{i+1}*row*when_{i}* row_{0}*i = number of rows - 1*? If the pattern is regular like that, you can use the same loop as you did earlier. You can use the modulus operator for the second term, i.e., (i + 1) % row, so that the index goes back to 0 once you reach the last row.### #7

## Re: Help with random number generator

Posted 14 November 2017 - 07:10 PM

How would I take the random numbers generated here

then multiply each number in the row by the next row? (ex. numbers in row 1 = row 1 numbers * row 2 numbers and so forth. The numbers in the last row will get multiplied by the numbers in the first row) I would like to display it in another table so the user sees the original and the multiplied table.

int main() { const int row = 10; const int col = 5; int arr[row][col]; srand(((unsigned)time(0))); cout << "Original" << endl; for (int i = 0; i<10; i++) { for (int j = 0; j<5; j++) { arr[i][j] = 1 + rand() % 99; cout << setw(4) << arr[i][j] << setw(5) << " | "; } cout << endl; }

then multiply each number in the row by the next row? (ex. numbers in row 1 = row 1 numbers * row 2 numbers and so forth. The numbers in the last row will get multiplied by the numbers in the first row) I would like to display it in another table so the user sees the original and the multiplied table.

### #8

## Re: Help with random number generator

Posted 14 November 2017 - 09:53 PM

Why don't you take a stab at it yourself? You already know how to loop through the array, and you know that you want to multiply each arr[i][j] by arr[i+1][j] and assign the result to arr[i][j] (since i represents the row coordinate and j represents the column coordinate of each element). The only complication occurs in the last row, and GazinAtCode already gave you a hint about that. If you don't know how to use the modulus operator for this, you can write an if statement to deal with the last row.

If you want to multiply a times b and assign the result to a you would write:

or more concisely:

So try to put all of that together & see what you can come up with.

If you want to multiply a times b and assign the result to a you would write:

a = a * b;

or more concisely:

a *= b;

So try to put all of that together & see what you can come up with.

### #9

## Re: Help with random number generator

Posted 14 November 2017 - 10:42 PM

I would suggest using another array for storing the results (as the OP was indicating) because when we're at the last row, the first one won't contain the original values anymore.

### #10

## Re: Help with random number generator

Posted 15 November 2017 - 06:48 AM

Good point.

Page 1 of 1