9 Replies - 636 Views - Last Post: 15 November 2017 - 06:48 AM Rate Topic: -----

#1 hh38985  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • Posts: 13,467
  • 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!
Was This Post Helpful? 0
  • +
  • -

#3 hh38985  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • 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.

Was This Post Helpful? 0
  • +
  • -

#4 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 69
  • 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;
}



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

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

Was This Post Helpful? 1
  • +
  • -

#5 hh38985  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 69
  • 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.
Was This Post Helpful? 1
  • +
  • -

#7 hh38985  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,387
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 GazinAtCode  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 69
  • 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.
Was This Post Helpful? 1
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 5,387
  • Joined: 27-December 05

Re: Help with random number generator

Posted 15 November 2017 - 06:48 AM

Good point.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1