Jump to content
  • 0

About random different item in a array


Question

Posted

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

14 answers to this question

Recommended Posts

  • 0
Posted

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
  • 0
Posted (edited)

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
  • 0
Posted (edited)

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
  • 0
Posted

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;
}
  • 0
Posted (edited)

 

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
  • 0
Posted

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?

  • 0
Posted
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

  • 0
Posted (edited)

 

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
  • 0
Posted

 

 

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...

  • 0
Posted

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.)
  • 0
Posted

 

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...

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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...