unique random number generator

I wanted a program too generate 9 random numbers. I think I've done this (or at least come close) but console just remains blank (cpu usage increases considerably). I've ran it for 5 minutes without a result. So I wanted to know if the program will never find an answer (because it hasn't been constructed properly) or it will just take a while.

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int checker (int,int,int,int,int,int,int,int,int);


int main()
{

int test[9];

int h;
srand (time(0));
int f =2;


for (int y=0; y<=9; y++){                                                               //generates 9 random numbers

h= 1+(rand()%9);
test[y] = h;



}



int bbc =20;
while(bbc == 20){                                                                    // stops while loop when unique random 9 integers found because bbc doesn't equal 20 if check returns 42 (which means it is unique)
int z =1;
while (z==1){

for(int x=0;x<=8;x++){

if (test[x+1]<test[x]){                                                            // moves every integer which is smaller than the integer on it's left to the left of that integer
int temp = test[x];
test [x] = test[x+1];
test [x+1] = temp;
}

}

if (test[1] >= test[0] && test[2] >= test[1] && test[3] >= test[2] && test[4] >= test[3] && test [5] >= test[4] && test [6] >=  test[5] && test[7] >= test[6] && test[8] >= test[7]){        //ends while loop when integers in order

z=2;
}
}

bbc =checker (test [0],test[1],test [2],test [3],test [4],test [5],test [6],test [7], test [8]);
}


cout << "hello";                                                         // outputs 'hello' when unique random number found.
}







int checker (int a, int b, int c, int d, int e, int f, int g, int h, int i){


int u;

if (a<b && b<c && c<d && d<e && e<f && f<g && h<i){                                          //checks to see if ordered array of integers unique

u =42;                                                                                      // if unique then u =42
}

else {

u =20;                                                                                      //else = 20;

}

return u;                                                                                       // returns value to checker function

}
¿why so obfuscated?
You are stepping out of bounds in line 36
If you want quasi random numbers, you can just do this:
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
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

short int nums[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1};
short int x = 0, y = 0;

int main()
{
    srand(time(NULL));
    x = 0;
    while(x < 9)
    {
        y = 0;
        nums[x] = rand() % 100 + 1; //Change the 100 to change the range. This currently has a range 1-100.
        while(y < 9)
        {
            if(nums[y] == nums[x] && x != y) //If another number is equal to this one.
            {
                --x; //Set x back one. 
                y = 0; //Reset y;
                break; //Try again.
            }
            else
            {
                 ++y; //Otherwise, check the next number.
            }
        }
        ++x; //When we reach here, if --x happened, we are about to re-do a number that was not unique. Otherwise, do the next number.
    }
    x = 0; //This next bit just outputs the numbers to the console. 
    while(x < 9)
    {
        cout << nums[x] << "  ";
        ++x;
    }
    return 0; //Sensible. 
}


This program should give an answer in a time measured in milliseconds.
Last edited on
You also have a boundary brake in line 21
Topic archived. No new replies allowed.