Jump to content
  • 0

About random different item in a array


hakuren

Question


  • Group:  Members
  • Topic Count:  120
  • Topics Per Day:  0.03
  • Content Count:  295
  • Reputation:   6
  • Joined:  12/02/11
  • Last Seen:  

Hi rAthena,

 

                                                                                            

 

Question :

- How can a npc script give 5 different items randomly in a array at the same-time.

 

                                                                                            

 

Example :

- setarray .@item_id, 501,502,503,504,505,506,507,508,509,510;

 

can someone help me?

 

                                                                                            

 

Thank you in advance  /thx

Link to comment
Share on other sites

14 answers to this question

Recommended Posts

  • 0

  • Group:  Members
  • Topic Count:  16
  • Topics Per Day:  0.00
  • Content Count:  658
  • Reputation:   663
  • Joined:  11/12/12
  • Last Seen:  

Just wanted to point out that

.@index = rand(0, getarraysize(.@item_ids) - 1);

is the equivalent of

.@index = rand(getarraysize(.@item_ids));

Except it's easier to read xD. Also, you should avoid using temporary char-bound variables in scripts (@item_id versus .@item_id), those are kept until the player logs out. The usage of "set" is also deprecated, you should be using 'variable = value;".

prontera,137,203,4	script	TEST::KST01	834,{
	setarray .@item_ids, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510;
	
	for (.@i = 0; .@i < 5; .@i++) {
		.@index = rand(getarraysize(.@item_ids));
		getitem .@item_ids[.@index], 1;
		deletearray .@item_ids[.@index], 1;
	}
	
	end;
}
  • Upvote 1
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  5
  • Topics Per Day:  0.00
  • Content Count:  21
  • Reputation:   1
  • Joined:  06/10/14
  • Last Seen:  

You need to use for loop.

Pseudocode:

for (i=0; i< 5; i++)
{
  getitem @item_id[rand(0,10)], 1;
}
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  120
  • Topics Per Day:  0.03
  • Content Count:  295
  • Reputation:   6
  • Joined:  12/02/11
  • Last Seen:  

You need to use for loop.

Pseudocode:

 

for (i=0; i< 5; i++)
{
  getitem @item_id[rand(0,10)], 1;
}

Hi @Kamii

thank you for your reply

 

i use this like this

setarray .@item_id, 501,502,503,504,505,506,507,508,509,510;

for (i=0; i< 5; i++)
{
  getitem .@item_id[rand(0,10)], 1;
}

 

yes it gives 5 items but... some on it give the same item when the random value is the same as the preview value

i need that 5 random items differently when receive by the player.

Edited by hakuren
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  5
  • Topics Per Day:  0.00
  • Content Count:  21
  • Reputation:   1
  • Joined:  06/10/14
  • Last Seen:  

Then you have to try this one.

 

NOTE: can't check if code is correct right now...
 

setarray .@item_id, 501,502,503,504,505,506,507,508,509,510;
for (@i=0; @i< 5; @i++)
{
  set @ind, rand(0,getarraysize(@item_id));
  getitem .@item_id[@ind], 1;
  deletearray @item_id[@ind],1;
}

 

Corrected Script:

prontera,137,203,4 script TEST 834,{
  setarray @item_id, 501,502,503,504,505,506,507,508,509,510;
  for (@i=0; @i< 5; @i++)
  {
    set @ind, rand(0, getarraysize(@item_id) - 1);
    getitem @item_id[@ind], 1;
    deletearray @item_id[@ind],1;
  }
}
Edited by Kamii
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  15
  • Topics Per Day:  0.00
  • Content Count:  99
  • Reputation:   10
  • Joined:  01/13/12
  • Last Seen:  

Hi rAthena,

 

                                                                                            

 

Question :

- How can a npc script give 5 different items randomly in a array at the same-time.

 

                                                                                            

 

Example :

- setarray .@item_id, 501,502,503,504,505,506,507,508,509,510;

 

can someone help me?

 

                                                                                            

 

Thank you in advance  /thx

prontera,155,125,4	script	test	4_m_genie,{
	setarray .@item_id[0],501,502,503,504,505,506,507,508,509,510;
	for(.@i=0; .@i<5;.@i++){
		.@pass = 0;
		while(1){
			// random item
			.@item = .@item_id[rand(0,getarraysize(.@item_id)-1)];
			if(getarraysize(.@del) != 0){
				for(.@j=0; .@j<getarraysize(.@del);.@j++){
					if(.@del[.@j]==.@item)	// repeat
						break;
					if(.@j == (getarraysize(.@del)-1))
						.@pass = 1;
				}
			} else {
				break;
			}			
			if(.@pass == 1)
				break;
		}
		getitem .@item,1;
		// record gived
		.@del[.@i] = .@item;
	}
	end;
}
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  120
  • Topics Per Day:  0.03
  • Content Count:  295
  • Reputation:   6
  • Joined:  12/02/11
  • Last Seen:  

 

Hi rAthena,

 

                                                                                            

 

Question :

- How can a npc script give 5 different items randomly in a array at the same-time.

 

                                                                                            

 

Example :

- setarray .@item_id, 501,502,503,504,505,506,507,508,509,510;

 

can someone help me?

 

                                                                                            

 

Thank you in advance  /thx

prontera,155,125,4	script	test	4_m_genie,{
	setarray .@item_id[0],501,502,503,504,505,506,507,508,509,510;
	for(.@i=0; .@i<5;.@i++){
		.@pass = 0;
		while(1){
			// random item
			.@item = .@item_id[rand(0,getarraysize(.@item_id)-1)];
			if(getarraysize(.@del) != 0){
				for(.@j=0; .@j<getarraysize(.@del);.@j++){
					if(.@del[.@j]==.@item)	// repeat
						break;
					if(.@j == (getarraysize(.@del)-1))
						.@pass = 1;
				}
			} else {
				break;
			}			
			if(.@pass == 1)
				break;
		}
		getitem .@item,1;
		// record gived
		.@del[.@i] = .@item;
	}
	end;
}

woooow! thank you @a91323 your script perfectly works! to my condition thankyou so much!!!

and thankyou @Kamii

Edited by hakuren
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  5
  • Topics Per Day:  0.00
  • Content Count:  21
  • Reputation:   1
  • Joined:  06/10/14
  • Last Seen:  

Keep in mind, that this script is really bad performance wise.
It's not wrong, but it's really not optimized.

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  120
  • Topics Per Day:  0.03
  • Content Count:  295
  • Reputation:   6
  • Joined:  12/02/11
  • Last Seen:  

Keep in mind, that this script is really bad performance wise.

It's not wrong, but it's really not optimized.

yeah i think it will give unlimited loop when the rand value return the same

correct me if im wrong or can you give me some idea to optimize this?

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  5
  • Topics Per Day:  0.00
  • Content Count:  21
  • Reputation:   1
  • Joined:  06/10/14
  • Last Seen:  

Maybe a91323 can optimizie his code by himself.
I'll be home in about 1 1/2 hour.
You could also try out my second code... I'll test it when i'm home.

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  120
  • Topics Per Day:  0.03
  • Content Count:  295
  • Reputation:   6
  • Joined:  12/02/11
  • Last Seen:  

setarray .@item_id, 501,502,503,504,505,506,507,508,509,510;
for (@i=0; @i< 5; @i++)
{
  set .@ind, rand(0,getarraysize(.@item_id));
  getitem .@item_id[.@ind], 1;
  deletearray .@item_id[.@ind],1;
}

 

 

i tried it but when the rand value return to previews deletearray value it return 0 and give error

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  15
  • Topics Per Day:  0.00
  • Content Count:  99
  • Reputation:   10
  • Joined:  01/13/12
  • Last Seen:  

 

setarray .@item_id, 501,502,503,504,505,506,507,508,509,510;
for (@i=0; @i< 5; @i++)
{
  set .@ind, rand(0,getarraysize(.@item_id));
  getitem .@item_id[.@ind], 1;
  deletearray .@item_id[.@ind],1;
}

 

 

i tried it but when the rand value return to previews deletearray value it return 0 and give error

 

because

.@item_id≠@item_id

and

set @ind, rand(0,getarraysize(.@item_id)-1);

deletearray <array name>[<first value>],<how much to delete>;
prontera,155,125,4	script	test	4_m_genie,{
	setarray .@item_id[0],501,502,503,504,505,506,507,508,509,510;
	for(.@i=0; .@i<5;.@i++){
		.@pass = 0;
		// random item
		.@item = .@item_id[rand(0,getarraysize(.@item_id)-1)];
		if(getarraysize(.@del) != 0){
			for(.@j=0; .@j<getarraysize(.@del);.@j++){
				if(.@del[.@j]==.@item)	// repeat
					break;
				if(.@j == (getarraysize(.@del)-1))
					.@pass = 1;
			}
		} else {
			.@pass = 1;
		}			
		if(.@pass == 1){
			getitem .@item,1;
			// record gived
			.@del[.@i] = .@item;
		} else {
			.@i -= 1;
		}
	}
	end;
}
Edited by a91323
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  120
  • Topics Per Day:  0.03
  • Content Count:  295
  • Reputation:   6
  • Joined:  12/02/11
  • Last Seen:  

 

 

setarray .@item_id, 501,502,503,504,505,506,507,508,509,510;
for (@i=0; @i< 5; @i++)
{
  set .@ind, rand(0,getarraysize(.@item_id));
  getitem .@item_id[.@ind], 1;
  deletearray .@item_id[.@ind],1;
}

 

 

i tried it but when the rand value return to previews deletearray value it return 0 and give error

 

because

.@item_id≠@item_id

and

set @ind, rand(0,getarraysize(.@item_id)-1);

deletearray <array name>[<first value>],<how much to delete>;
prontera,155,125,4	script	test	4_m_genie,{
	setarray .@item_id[0],501,502,503,504,505,506,507,508,509,510;
	for(.@i=0; .@i<5;.@i++){
		.@pass = 0;
		// random item
		.@item = .@item_id[rand(0,getarraysize(.@item_id)-1)];
		if(getarraysize(.@del) != 0){
			for(.@j=0; .@j<getarraysize(.@del);.@j++){
				if(.@del[.@j]==.@item)	// repeat
					break;
				if(.@j == (getarraysize(.@del)-1))
					.@pass = 1;
			}
		} else {
			.@pass = 1;
		}			
		if(.@pass == 1){
			getitem .@item,1;
			// record gived
			.@del[.@i] = .@item;
		} else {
			.@i -= 1;
		}
	}
	end;
}

wow another knowledge acquired thankyou for the information :) thank you again...

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  5
  • Topics Per Day:  0.00
  • Content Count:  21
  • Reputation:   1
  • Joined:  06/10/14
  • Last Seen:  

Alright i corrected my script.

prontera,137,203,4 script TEST::KST01 834,{
  setarray @item_id, 501,502,503,504,505,506,507,508,509,510;
  for (@i=0; @i< 5; @i++)
  {
    set @ind, rand(0, getarraysize(@item_id) - 1);
    getitem @item_id[@ind], 1;
    deletearray @item_id[@ind],1;
  }
}
It works now as intented. (If someone else needs that kind of script.)
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  5
  • Topics Per Day:  0.00
  • Content Count:  21
  • Reputation:   1
  • Joined:  06/10/14
  • Last Seen:  

 

Just wanted to point out that

.@index = rand(0, getarraysize(.@item_ids) - 1);

is the equivalent of

.@index = rand(getarraysize(.@item_ids));

Except it's easier to read xD. Also, you should avoid using temporary char-bound variables in scripts (@item_id versus .@item_id), those are kept until the player logs out. The usage of "set" is also deprecated, you should be using 'variable = value;".

prontera,137,203,4	script	TEST::KST01	834,{
	setarray .@item_ids, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510;
	
	for (.@i = 0; .@i < 5; .@i++) {
		.@index = rand(getarraysize(.@item_ids));
		getitem .@item_ids[.@index], 1;
		deletearray .@item_ids[.@index], 1;
	}
	
	end;
}

That Sir, are some useful informations.

I did my last script on 2007 or 2008... So my knowledge is pretty old on this one.

I'm gonna have to redo my new scripts...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...