Jump to content
  • 0

Copy random array


Shinto

Question


  • Group:  Members
  • Topic Count:  3
  • Topics Per Day:  0.00
  • Content Count:  15
  • Reputation:   1
  • Joined:  12/16/17
  • Last Seen:  

hi everyone .. sorry my question a little trivial but unfortunately I can not come to head ...
in practice I should copy the values of an array in another array but in a different arrangement (random type).

Place an image to make you understand better why my English is not the best ... :mellow:

grafico.png

I tried to do it this way but logically it can happen that it copies the same value twice in the array

@n = query_sql("SELECT `name` FROM `char`", .@name$);
		for(.@na = 0; .@na < @n ; .@na++){ 
			.@rand_array = rand(0,@n);
			copyarray .@sorteggio$[.@na],.@name$[.@rand_array],@n;
		}
 

surely there are errors, I would be grateful if someone explained me a little better ...

if I have not explained myself well I will try to make myself understood better
^_^

Edited by Shinto
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 1

  • Group:  Members
  • Topic Count:  18
  • Topics Per Day:  0.00
  • Content Count:  2044
  • Reputation:   682
  • Joined:  10/09/12
  • Last Seen:  

are you trying to shuffle array ?

like 1234 becomes 2134 or 3142 like this ?

 

EDIT: I want to make sure I understand what he is saying, or else if I bring up my shuffle algorithm script and sorting algorithm script, this can scare some people away

 

EDIT2: you mean something like

.arr[0] = 123; <-- 1st index

.arr[1] = 234; <-- 2nd index

.arr[2] = 345; <-- 3rd index

to become something like

.arr2[0] = 345; <-- 3rd index from original array become 1st index

.arr2[1] = 123; <-- 1st index from original array become 2nd index

.arr2[2] = 234; <-- 2nd index from original array become 3rd index

Edited by AnnieRuru
Link to comment
Share on other sites

  • 1

  • Group:  Members
  • Topic Count:  18
  • Topics Per Day:  0.00
  • Content Count:  2044
  • Reputation:   682
  • Joined:  10/09/12
  • Last Seen:  

the reason why I do not immediately answer your question because you are asking a high level scripting question

I experienced in the past when I misunderstood topic starter, and I posted a very complicated script, they just stop giving any feedback ... makes me feel my effort wasted

 

what you need to learn is Fisher-Yates shuffle

Haru and me perfected the script and it is currently inside Hercules

 

here's an example

poring_w01,100,100,5	script	dfdsfsadfsdf	1_F_MARIA,{
	// here we declare some array
	setarray .@tmp, 123,234,345,456,567,678,789;
	.@size = getarraysize( .@tmp );

	// just to output the original array
	.@string$ = .@tmp[0] +"";
	for ( .@i = 1; .@i < .@size; ++.@i )
		.@string$ += ","+ .@tmp[.@i];
	dispbottom "original = "+ .@string$;

	// randomize the array ~
	callfunc "F_ShuffleNumbers", 0, .@size -1, .@r;
	for ( .@i = 0; .@i < .@size; ++.@i )
		.@tmp2[.@i] = .@tmp[ .@r[.@i] ];

	// output the result ~
	.@string$ = .@tmp2[0] +"";
	for ( .@i = 1; .@i < .@size; ++.@i )
		.@string$ += ","+ .@tmp2[.@i];
	dispbottom "shuffle = "+ .@string$;

	end;
}

 

 

EDIT : PS: if you wanna learn sorting algorithm script I can always show off a little bit /gg

Edited by AnnieRuru
  • Upvote 1
  • MVP 1
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  3
  • Topics Per Day:  0.00
  • Content Count:  15
  • Reputation:   1
  • Joined:  12/16/17
  • Last Seen:  

yes, I try to explain myself better ...
let's say that I have two arrays one is called array_1 and another array_2
array_1 has these values
example:
Array_1 [0] = 1;
Array_1 [1] = 2;
array_1 [2] = 3;

now I would like to copy the value of the array_1 in the array_2 but not in sequence but mixed
Example
Array_2 [0] ---> contents of Array_1 [2] (Array_2 [0] = 3;)
Array_2 [1] ---> contents of Array_1 [0] (Array_2 [1] = 1;)
Array_2 [2] ---> contents of Array_1 [1] (Array_2 [1] = 2;)

I do not know if you understood unfortunately I can not explain well in this example ...

in practice I have to randomly take an index of array_1 and copy it to array_2

 

----------------------------------------------------

EDIT

reading well I know that you understand ... I do not want to confuse your ideas

Edited by Shinto
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  3
  • Topics Per Day:  0.00
  • Content Count:  15
  • Reputation:   1
  • Joined:  12/16/17
  • Last Seen:  

thanks so much Annie I solved thanks to your help
Edited by Shinto
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...