Random nubers

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
#include <cstdlib>
#include <time.h>
#include <iostream>
using namespace std;

int main() {

	//initialize random seed
	srand(time(NULL));

	unsigned columns, rows; //Define variables of array
	cout << "Number of rows is : ";
	cin >> rows; // Getting value from user
	cout << "Number of columns is : ";
	cin >> columns; // Getting value from user

	unsigned array[rows][columns];// Define an array
	unsigned i, j;// Define i, j

	for(i = 0 ; i < rows ; i++){
		for(j = 0 ; j < columns ; j++){
			array[i][j] = 1;
			array[i][j] = (random() % rows*columns)+1;//set random value to array
			cout << array[i][j] << " ";
		}
	}

	return 0;
}


I want to make a program that giving the matrix element a random number without repeat
Last edited on
closed account (o3hC5Di1)
Hi there,

I believe the correct function name is rand();, not random();
Also, I would increment rows and columns by 1 when using the modulus operator - the first iteration you would be dividing by zero otherwise, which is not allowed.

All the best,
NwN
Last edited on
Thanks @MwN

but I think they are the same
@OP:

Which IDE or text editor do you use?
Eclipse
closed account (o3hC5Di1)
http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

Then what is it that isn't working?
Are you getting compiler errors - if so, which ones?
Is the output not what you expected, if so, what are you expecting and what is the output?

All the best,
NwN
I want to make a program that giving the matrix element a random number without repeat
rand(); uses an algorithm which I think cannot be changed. so you do not have control over repetition of numbers within the rang you have specified.
Last edited on
@Acelix

I want to make a program that giving the matrix element a random number without repeat

how can I do this ???

if you know help me ^_^
Last edited on
closed account (o3hC5Di1)
If you want to make sure the numbers are unique, you'll need to store them in an array and check every number generated to all the numbers in the array. If the number is in the array, generate a new random number and check again.

All the best,
NwN
Last edited on
NwN's solution would work best in this situation.

Here is something that will do what NwN is suggesting:

you'll need to include unordered_set

1
2
3
4
5
6
int rows = 5;
int columns = 5;

unordered_set<int> randNumbers;
while(randNumbers.size() < (rows*columns))
	randNumbers.insert((rand()%100)+1);


This assumes user input is 5x5 matrix. So the code will fill a set with unique random numbers until it reaches the user defined matrix size.

This unordered_set size will be 25 so all what's left to do is iterate through and add the numbers from the unordered_set into the matrix as you loop through the rows and columns in your line 23.

Hope that helps,
Anthony
Last edited on
1
2
3
4
5
6
7
8
9
unsigned array[rows*columns];

//fill the "matrix" 1 to rows*columns
for(unsigned K=0; K<rows; ++K)
	for(unsigned L=0; L<columns; ++L)
		array[K*columns+L] = K*columns+L+1;

//reorganize the data in a random way
std::random_shuffle(array, array+rows*columns);
If you insist in using a multidimensional array, cast the parameters
Topic archived. No new replies allowed.