Gamestudio Links
Zorro Links
Newest Posts
ZorroGPT
by TipmyPip. 03/01/26 16:40
WFO Training with parallel cores Zorro64
by Martin_HH. 02/26/26 16:03
Zorro version 3.0 prerelease!
by TipmyPip. 02/25/26 16:38
Camera always moves upwards?
by clonman. 02/21/26 09:29
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 02/19/26 13:22
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
4 registered members (TipmyPip, the1, AndrewAMD, Quad), 5,068 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
the1, alx, ApprenticeInMuc, PatrickH90, USER0328
19200 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
While statements #320409
04/21/10 16:22
04/21/10 16:22
Joined: Apr 2010
Posts: 20
I
Icarni Offline OP
Newbie
Icarni  Offline OP
Newbie
I

Joined: Apr 2010
Posts: 20
I'm trying to write a function that creates 10 unique random numbers and I keep getting a syntax error on the while statement. Here's the code I'm writing:

function x_random()
{
while(x_1 == x_2) || (x_2 == x_3) || (x_3 == x_4) || (x_4 == x_5) || (x_5 == x_6) || (x_6 == x_7) || (x_7 == x_8) || (x_8 == x_9) || (x_9 == x_10) || (x_1 == x_3) || (x_2 == x_4) || (x_3 == x_5) || (x_4 == x_6) || (x_5 == x_7) ||
(x_6 == x_8) || (x_7 == x_9) || (x_8 == x_10) || (x_1 == x_4) || (x_2 == x_5) || (x_3 == x_6) || (x_4 == x_7) ||
(x_5 == x_8) || (x_6 == x_9) || (x_7 == x_10) || (x_1 == x_5) || (x_2 == x_6) || (x_3 == x_7) || (x_4 == x_8) ||
(x_5 == x_9) || (x_6 == x_10) || (x_1 == x_6) || (x_2 == x_7) || (x_3 == x_8) || (x_4 == x_9) || (x_5 == x_10) ||
(x_1 == x_7) || (x_2 == x_8) || (x_3 == x_9) || (x_4 == x_10) || (x_1 == x_8) || (x_2 == x_9) || (x_3 == x_10) ||
(x_1 == x_9) || (x_2 == x_10) || (x_1 == x_10)
{
random_seed(0);
x_1 = integer(random(192)+1);
x_2 = integer(random(192)+1);
x_3 = integer(random(192)+1);
x_4 = integer(random(192)+1);
x_5 = integer(random(192)+1);
x_6 = integer(random(192)+1);
x_7 = integer(random(192)+1);
x_8 = integer(random(192)+1);
x_9 = integer(random(192)+1);
x_10 = integer(random(192)+1);
wait(1);
}

}

What am I doing wrong here?

Re: While statements [Re: Icarni] #320411
04/21/10 16:31
04/21/10 16:31
Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
Rei_Ayanami Offline
Expert
Rei_Ayanami  Offline
Expert

Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
you need to set one () around all statements:

while((x_1 == x_2) | ...... | (x_1 == x_10))

Re: While statements [Re: Icarni] #320412
04/21/10 16:34
04/21/10 16:34
Joined: Sep 2009
Posts: 496
P
Progger Offline
Senior Member
Progger  Offline
Senior Member
P

Joined: Sep 2009
Posts: 496
try this
Code:
while((x_1 == x_2) || (x_2 == x_3) || (x_3 == x_4) || (x_4 == x_5) || (x_5 == x_6) || (x_6 == x_7) || (x_7 == x_8) || (x_8 == x_9) || (x_9 == x_10) || (x_1 == x_3) || (x_2 == x_4) || (x_3 == x_5) || (x_4 == x_6) || (x_5 == x_7) ||
(x_6 == x_8) || (x_7 == x_9) || (x_8 == x_10) || (x_1 == x_4) || (x_2 == x_5) || (x_3 == x_6) || (x_4 == x_7) ||
(x_5 == x_8) || (x_6 == x_9) || (x_7 == x_10) || (x_1 == x_5) || (x_2 == x_6) || (x_3 == x_7) || (x_4 == x_8) ||
(x_5 == x_9) || (x_6 == x_10) || (x_1 == x_6) || (x_2 == x_7) || (x_3 == x_8) || (x_4 == x_9) || (x_5 == x_10) ||
(x_1 == x_7) || (x_2 == x_8) || (x_3 == x_9) || (x_4 == x_10) || (x_1 == x_8) || (x_2 == x_9) || (x_3 == x_10) ||
(x_1 == x_9) || (x_2 == x_10) || (x_1 == x_10))



You just frogot two brackets
WFG Progger laugh


asking is the best Way to get help laugh laugh laugh
Re: While statements [Re: Progger] #320413
04/21/10 16:37
04/21/10 16:37
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
that's a very bad way of doing this. tongue

Re: While statements [Re: Progger] #320414
04/21/10 16:37
04/21/10 16:37
Joined: Apr 2010
Posts: 20
I
Icarni Offline OP
Newbie
Icarni  Offline OP
Newbie
I

Joined: Apr 2010
Posts: 20
The devil's in the fricken details...

Thanks guys!

Re: While statements [Re: Icarni] #320415
04/21/10 16:38
04/21/10 16:38
Joined: Apr 2010
Posts: 20
I
Icarni Offline OP
Newbie
Icarni  Offline OP
Newbie
I

Joined: Apr 2010
Posts: 20
I'm open to suggestion ventilator, what's a better way to do this?

Re: While statements [Re: Icarni] #320416
04/21/10 16:42
04/21/10 16:42
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
create a list that contains your number range and remove 10 random numbers from that list. or something similar... your algorithm has an undefined runtime.

Re: While statements [Re: ventilator] #320417
04/21/10 16:49
04/21/10 16:49
Joined: Apr 2010
Posts: 20
I
Icarni Offline OP
Newbie
Icarni  Offline OP
Newbie
I

Joined: Apr 2010
Posts: 20
Ok, I feel truly stupid asking this question, but how do you create a list?

Re: While statements [Re: Icarni] #320418
04/21/10 16:59
04/21/10 16:59
Joined: Oct 2007
Posts: 5,211
İstanbul, Turkey
Quad Online
Senior Expert
Quad  Online
Senior Expert

Joined: Oct 2007
Posts: 5,211
İstanbul, Turkey
you may also try to use an array.

Code:
void main(){
	video_screen = 0;
	
	var vx[10];
	int i = 0;
	int j = 0;

	while(i<10){
		vx[i] = integer(random(192)+1);//create random number
		//check if that number generated before
		j = 0;
		while(j<10){
			//loop thru all numbers
			if(i!=j)if(vx[i]==vx[j]) break; 
			j++;
		}
		if(j==10) i++;//if j is 10 at this point it means loop is not broken, therefore same number not encountered, start generating nex number
	}	
	

	//print all numbers
	i = 0;
	while(i<10){
		printf("%d \n",vx[i]);
		i++;
	}
	
}




3333333333
Re: While statements [Re: Icarni] #320419
04/21/10 17:07
04/21/10 17:07
Joined: May 2002
Posts: 7,441
ventilator Offline
Senior Expert
ventilator  Offline
Senior Expert

Joined: May 2002
Posts: 7,441
creating a list actually is a bit cumbersome with a low level language like lite-c.

but you could also try something like that instead:

Code:
int array[192]; // fill it with 1..192
int numbers[10];
int n;

for(n=0; n<10; n++)
{
    index = random(191-n);
    numbers[n] = array[index];
    array[index] = array[191-n]; // remove the number from the array by replacing it with the last one in the array (the last one won't be accessed anymore)
}



it's not tested. probably it needs some small corrections (integer(),...) but you should get the idea.



edit: didn't see quadraxas' solution before doing my post. i think mine is better and more elegant. tongue quadraxas' solution also has random runtime. i think it's better than the original huge while clause though.

Page 1 of 3 1 2 3

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1