How to Generate a random array in C ++ , with 100 different numbers

hi guys, i'm learning c++ and I have problems how to create a random board[10][10], in C++ with 100 differents numbers.


my actual code is this, but I don't know why it don't work :
int x,y,x2,y2;
int valid;

for (x=0;x<N;x++)
for(y=0; y<N; y++){
do{
tabul[x][y]=rand()%100;
for (x2=0;x2<N;x2++)
for(y2=0; y2<N; y2++){
if (x==x2 && y==y2 && tabul[x][y]==tabul[x2][y2] ) valid=0;
}

}while(!valid);




I aprecciate your time and your help!
first code allowing duplicates.
but the first code make a random and then checks if it has the same number and if it has, make an random, no?
no, i meant, program it without checking. fill the array with randoms (same number can appear multiple times)
exactly but what need I do to check if it duplicates and make an random till has no number twice?
if you program as arr1[100] instead of arr2[10][10] it'll be easier

if you must need 2d array you can copy arr1 to arr2 - its easier than directly create by checking arr2
Fill the array in sequentially and then shuffle it.
> Fill the array in sequentially and then shuffle it.

According to me, this is the best strategy. You are guaranteed that every number is unique and you add randomization through shuffling.
Implementation should be fairly straight-forward:
- a for-loop to fill the array
- a random number of exchanges which involve two randomly selected indexes
That's all.

If you need a matrix structure (i.e. a bi-dimensional array), you can do as we said with a mono-dimensional array, then you can fill a matrix with the values you obtain from the array.
Last edited on
> Fill the array in sequentially and then shuffle it.

> +1 for this example

> but learning how to check is important. as many times they wont be inherited from a serial.

> the above way technically isn't random. if you see first 99 numbers you can surely tell what 100th number is !
i think i found the solution:

int x,y,x2,y2,i;

int v[100];
for (i=0; i<100; i++)
v[i]=i;


for( x2=0; x2<N; x2++)

for ( y2=0; y2<N; y2++){

do{ i=rand()%100;

} while(v[i]==-1);

tabul[y2][x2]=v[i];
v[i]=-1;
}
Ok, I haven't tried your code but is seems ok. I just want you to notice one thing: you say

do {
pick a random number
} while (this number is not good)

Well... since rand is random (actually pseudo-random but anyway...) you are not guaranteed that this code will be able to terminate. Imagine this situation: you have your array which is

n -1 -1 -1 ....................................... -1

where n is the last element you have to put in the matrix and is located at position 0. Then you are not guaranteed that sooner or later 0 will be picked by the rand function. Actually this happens (probably after a while) but it is not 100% reliable.

Instead the solution we were discussing before is this one:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <cstdlib>
#include <ctime>

void swap(int array[], int pos1, int pos2) {
	int tmp = array[pos1];
	array[pos1] = array[pos2];
	array[pos2] = tmp;
}

int main() {

	srand(time(NULL));

	int numbers[100];
	int matrix[10][10];

	// fill in the numbers
	for (int i = 0; i < 100; ++i) {
		numbers[i] = i;
	}

	// a random number of swaps
	int repeat = rand() % 100 + 100;

	// perform the swaps...
	for (int i = 0; i < repeat; ++i) {
		// ... with random positions
		swap(numbers, rand() % 100, rand() % 100);
	}

	// fill in the matrix
	for (int i = 0; i < 10; ++i) {
		for (int j = 0; j < 10; ++j) {
			matrix[i][j] = numbers[i * 10 + j];
		}
	}

	std::cout << "This is the matrix:" << std::endl << std::endl;

	// print the matrix
	for (int i = 0; i < 10; ++i) {
		for (int j = 0; j < 10; ++j) {
			std::cout << matrix[i][j] << "  ";
		}
		std::cout << std::endl;
	}

	return 0;
}


And here there is no guessing: it starts, works and completes. Always! :)
Last edited on
thanks a lot guys ! :)
Topic archived. No new replies allowed.