help randomizing an array c++

Hi,
i need help in making a 3x3 grid with the numbers 1-9 and they need to be in a random order without any repeating numbers.

so far i have been able to make the 3x3 grid with the numbers and i have also been able to make a list of numbers 1-9 in a random order but i can't seam to implement the two together.

any advice on how to do this would be greatly appreciated. thanks

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
51
52
#include<iostream>
#include<conio.h>
#include<string>
#include<cstdlib>
#include<ctime>
#include <time.h>
#include <stdio.h>

using namespace std;
int main ()
{


char numb1array[3][3] = { '1','2','3','4','5','6','7','8','9',};
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout << numb1array[i][j];
}
cout << endl;
}




		
	const int ARRAY_SIZE = 9;
		srand(time(0));

		string numbers[ARRAY_SIZE] =  {"1","2","3","4","5","6","7","8","9"};

		for (int i=0;  i< ARRAY_SIZE; i++) {

			int index = rand() % ARRAY_SIZE;

			string temp = numbers[i];
			numbers[i] = numbers [index];
			numbers[index] = temp;
		}

		for (int i=0;  i< ARRAY_SIZE; i++) {
			cout <<  numbers[i] << endl;
		}






_getch();
}
You could do something like this if you want no repeating numbers

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
 
//libraries
bool function (int check) // check if the num has been picked before
{
   int temp [9]; 
   for (int a = 0; a < 9; a++)
   {
           if (check == temp [a]) 
             { 
                return false;
              } 
    }
 temp [check -1] = check; 
return true; 
}
// 80% sure this function will work sorry if not... 

int main () 
{
int randomnum; // store srand num
int b = 0; // this will be used as counter for the grid 
int counter = 0; // ensures the program can't get stuck in a infinite loop
bool checkee = false; 
while (counter < 9) 
{
while (checkee = false;) 
{
// pick random num between 1 - 9 (it appears you know how to use srand so im not gonna bother.)
checkee = function (randomnum); 
} 
if (b = 2) 
{ 
cout << randomnum << endl; 
counter ++; 
b = 0; 
}
else 
{
cout << randomnum << " "; 
counter ++; 
b++; 
}
}
return 0; 
} 

This isn't really a efficient method sorry, but Im pretty confident it works and solves your problem.
Last edited on
Actually on a second thought if we assume the new values are correctly slotted into the array after they are used then the function could go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
bool function (int check) // check if the num has been picked before
{
   int temp [9]; 
           if (check == temp [check - 1]) 
             { 
                return false;
              } 
          else 
             {
                 temp [check -1] = check; 
                return true; 
             }
}


and instead of using b you could just store the values in a two d array then print them off up to you. However, the 2d array would probably be better... and it appears it is what you intended to do.
Last edited on
i can't get it to work.

the reason i need it is because i need to make a game where the users guesses the number on the 3x3 grid so it needs to be a random generated number each time
can't you just call random_shuffle()?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <algorithm>

int main ()
{
    char numb1array[3][3] = { '1','2','3','4','5','6','7','8','9',};

    std::random_shuffle( &numb1array[0][0], &numb1array[0][0]+9 );

    for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 3; ++j)
        {
            std::cout << numb1array[i][j];
        }
        std::cout << '\n';
    }
}
random_shuffle() didn't work.

it did mix up the order. but when running the application again it would't mix it up again.
@l0311 you may want to seed the random generator it uses, in most cases it uses rand(), so you have to run srand() some time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	const int ARRAY_SIZE = 9;
		srand(time(0));

		string numbers[ARRAY_SIZE] =  {"1","2","3","4","5","6","7","8","9"};

		for (int i=0;  i< ARRAY_SIZE; i++) {

			int index = rand() % ARRAY_SIZE;

			string temp = numbers[i];
			numbers[i] = numbers [index];
			numbers[index] = temp;
		}

		for (int i=0;  i< ARRAY_SIZE; i++) {
			cout <<  numbers[i] << endl;
		}


in this bit of code i have used rand() and srand() to make the numbers random.


the problem with this is that it displays it each time on a new line like this:

9
4
2
3
6
5
1
3
7


whereas i would need it to display on a 3x3 grid similar to this:

9 4 2
3 6 5
1 3 7
Topic archived. No new replies allowed.