# Help with random number generator

Page 1 of 1

## 9 Replies - 807 Views - Last Post: 15 November 2017 - 06:48 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407471&amp;s=8d9ebe1e6a915b10ff85347cbc4f1dd0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hh38985

Reputation: 0
• Posts: 12
• Joined: 06-November 17

# 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?

```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;
}
```

Is This A Good Question/Topic? 0

## Replies To: Help with random number generator

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3796
• Posts: 13,742
• Joined: 08-August 08

## 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 hh38985

Reputation: 0
• Posts: 12
• Joined: 06-November 17

## 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 GazinAtCode

Reputation: 33
• Posts: 121
• Joined: 26-September 16

## 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):

```#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;
}

```

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

### #5 hh38985

Reputation: 0
• Posts: 12
• Joined: 06-November 17

## 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 GazinAtCode

Reputation: 33
• Posts: 121
• Joined: 26-September 16

## Re: Help with random number generator

Posted 14 November 2017 - 12:30 AM

You mean something like rowi * rowi+1 and then rowi * row0 when 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 hh38985

Reputation: 0
• Posts: 12
• Joined: 06-November 17

## Re: Help with random number generator

Posted 14 November 2017 - 07:10 PM

How would I take the random numbers generated here
```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 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## 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:
```a = a * b;
```

or more concisely:
```a *= b;
```

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

### #9 GazinAtCode

Reputation: 33
• Posts: 121
• Joined: 26-September 16

## 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 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: Help with random number generator

Posted 15 November 2017 - 06:48 AM

Good point.