Link to home
Start Free TrialLog in
Avatar of Xadina
Xadina

asked on

generating random numbers

I have a 5 x 5 grid of buttons which display OK at the moment.
I would like each button to display a random number from 1 to 25 so that all buttons are distincly numbered. How can i do this?

            case ID_BOARD_FIVE:
            HWND hwndButton;
            int x;
            int y;
           
            for (x=0; x<5; x++)
            for (y=0; y<5; y++) {
            hwndButton = CreateWindow ("BUTTON", "OK",
            WS_CHILD | WS_VISIBLE,
            20+x*37, 20+y*37, 35, 35,
            hwnd,
            (HMENU)"", NULL, NULL);
            }
            break;
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

- make an int array of 25
- fill all with numbers from 1 to 25 (ordered)
- repeat 100 times this:
     select two random numbers from 1 to 25
     interchange both array elements at the random indexes generated
- now you have a list of 25 items un-ordered, use this numbers to generate button labels
Avatar of Fippy_Darkpaw
Fippy_Darkpaw

#include <cstdlib>

int getRandomInt(int min, int max)
{
    int range = (max – min);
    return ( (range*rand()) / (RAND_MAX+1)) + min;
}

Or you can do it this. Less code but slower when running:

int getRandomInt(int min, int max)
{
    int range = (max – min);
    return ( rand()%range ) + min;
}

Should probably inline those too since they are short.
The getRandomInt answer won't work because there is nothing to prevent duplicates. Each button needs to contain a distinct number, but there is nothing to stop getRandomInt returning eg 21 twice in 25 calls.
That's why I created an ordered list, random algorithm is used to dis-order the array, not to obtain numbers between 1-25

int label[25];
int a, b, temp;

for (int cont=0; cont<25; cont++)
     label[cont] = cont+1;

for (cont=0; cont<100; cont++)  // I think 100 times will be statistical enough
{
      a = rand()%25;
      b = rand()%25;
      t = label[a];
      label[a] = label[b];
      label[b] = t;
}

// now array is disordered
Avatar of Xadina

ASKER

So now I put 't' instead of 'ok' in CreateWindow?

for (x=0; x<5; x++)
            for (y=0; y<5; y++) {
            hwndButton = CreateWindow ("BUTTON", t,
            WS_CHILD | WS_VISIBLE,
            20+x*37, 20+y*37, 35, 35,
            hwnd,
            (HMENU)"", NULL, NULL);
            }
            break;

what should i write instead of ""?
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Xadina

ASKER

The above code compiles and runs ok. My only problem is that everytime i run it, the numbered buttons always appear in the same order i.e. 13, 8, 24...
I would like the numbers to change positions every time i run it.
put this line after char txtlabel[3]:

srand( (unsigned)time( NULL ) );
Avatar of Xadina

ASKER

Thanks.  but i have another small problem.
what if i want an 8 by 8 grid?

int labelb[64];

for (int cont=0; cont<64; cont++)
     labelb[coant] = cont+1;

for (cont=0; cont<100; cont++)  
{
      a = rand()%64;
      b = rand()%64;
      t = label[a];
      labelb[a] = labelb[b];
      labelb[b] = t;
}

char txtlabelb[3];

for (x=0; x<8; x++)
    for (y=0; y<8; y++) {
            sprintf(txtlabelb, "%i", label[x*5+y]);
            hwndButton = CreateWindow ("BUTTON", txtlabelb,
            WS_CHILD | WS_VISIBLE,
            20+x*37, 20+y*37, 35, 35,
            hwnd,
            (HMENU)"", NULL, NULL);
}

It runs ok but does not give numbers from 1 to 64. It generates strange numbers.
Pay attention to this:

     labelb[cont] = cont+1; // I guess it is a typing error

     sprintf(txtlabelb, "%i", label[x*8+y]);    // Multiply by 8 this time
Avatar of Xadina

ASKER

I changed the mistakes but it is still giving me strange numbers like 273, 4242 and other large numbers that do not "fit" in the button.
     t = label[a];

must be

      t = labelb[a];

BTW. I made a mistake:
srand( (unsigned)time( NULL ) );
must be after
int labelb[64]; (or int label[25])

Avatar of Xadina

ASKER

I also found another mistake :

sprintf(txtlabelb, "%i", label[x*8+y]);

I forgot to change label to label b.

Thanks. Now everything works ok.
Now I will take a rest