In my app, you get a start button first, when you hit "start" the app will randomize the ints fraga1-fraga40 between 0 and 23(which will be used as questions). fraga1 to fraga12 can't be the same. fraga13 to fraga22 can't be the same. fraga23 to fraga28 can't be the same. fraga29 to fraga40 can't be the same.
then after a while, you get another start button.. when you press that one, fraga41-fraga81 will be randomized between 0 and 23. fraga41 to fraga52 can't be the same.`` fraga53 to fraga62 can't be the same. fraga63 to fraga68 can't be the same. fraga69 to fraga80 can't be the same.
but when you click the start button the second time, the button stays"clicked"(or "highlighted") and the app simulator freezes.
i am using a code like this...
-(void) v1
{
fraga1=arc4random()%24;
[self v2];
}
-(void) v2
{
fraga2=arc4random()%24;
if(fraga2==fraga1)
{
[self v2];
}
[self v3];
}
-(void) v3
{
fraga3=arc4random()%24;
if(fraga3==fraga2||fraga3==fraga1)
{
[self v3];
}
[self v4];
}
all the way to 40 from the first button... and from v41 to v80 from the other button!
Do you think I can fix this? Should I change my strategy for randomizing the questions?
I have tried to change it so it will randomize like 30 numbers, but it is still the same.. when I try like 100 numbers on each question.. it works, but is kind of slow!
Thanks in advance!
/a noob
EDIT: I did this, but every fraga is set to 0:
void fillUniqueRand(uint32_t arr[], uint32_t l, uint32_t n)
{ uint32_t in, il = 0;
for(in = 0; in < n && il < l; ++in)
{
uint32_t rn = n - in;
uint32_t rl = l - il;
if(arc4random() % rn < rl)
arr[il++] = in;
}
//We now have random numbers in order and
//need to shuffle them
uint32_t j, tmp;
for(uint32_t i = l - 1; i > 0; i--)
{
j = arc4random() % (i + 1);
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
} //Calling this from a method -(void) vnr { uint32_t fraga1, fraga2, fraga3,fraga4, fraga5,fraga6,fraga7,fraga8,fraga9,fraga10,fraga11,fraga12, fraga13,fraga14,fraga15,fraga16,fraga17,fraga18,fraga19,fraga20,fraga21,fraga22, //... fraga23, fraga24, fraga25,fraga26,fraga27,fraga28,fraga29,fraga30,fraga31,fraga32,fraga33,fraga34,fraga35,fraga36,fraga37,fraga38,fraga39,fraga40; //,... ;
//Max fraga range I noticed was 12
uint32_t unique[12] = { 0 };
fillUniqueRand(unique, 12u, 24u);
fraga1 = unique[0];
fraga2 = unique[1];
fraga3 = unique[2];
fraga4 = unique[3];
fraga5 = unique[4];
fraga6 = unique[5];
fraga7 = unique[6];
fraga8 = unique[7];
fraga9 = unique[8];
fraga10 = unique[9];
fraga11 = unique[10];
fraga12 = unique[11];
//...
fillUniqueRand(unique, 10, 24u);
fraga13 = unique[0];
fraga14 = unique[1];
fraga15 = unique[2];
fraga16 = unique[3];
fraga17 = unique[4];
fraga18 = unique[5];
fraga19 = unique[6];
fraga20 = unique[7];
fraga21 = unique[8];
fraga22 = unique[9];
//Only 6 elements between fraga23-fraga28
fillUniqueRand(unique, 6, 21u);
fraga23 = unique[0];
fraga24 = unique[1];
fraga25 = unique[2];
fraga26 = unique[3];
fraga27 = unique[4];
fraga28 = unique[5];
fillUniqueRand(unique, 12u, 24u);
fraga29 = unique[0];
fraga30 = unique[1];
fraga31 = unique[2];
fraga32 = unique[3];
fraga33 = unique[4];
fraga34 = unique[5];
fraga35 = unique[6];
fraga36 = unique[7];
fraga37 = unique[8];
fraga38 = unique[9];
fraga39 = unique[10];
fraga40 = unique[11];
//...
//You get the picture
}