(Solved!)How to create and Random Shuffle Array?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Location: Italy
Contact:

(Solved!)How to create and Random Shuffle Array?

Post by Fasasoftware » Wed Oct 24, 2018 6:51 pm

Dear friends,
i need to create and random array from an array like this:
or create from zero an array from 1 to 16 but i need inside this "tarraysuoni" the values of 1 to 8 and repeated another time from 1 to 8....
This is my example that i need...:

Code: Select all


tlistasuoni [16] 

 put  1 into tlistasuoni[1]
 put  2 into tlistasuoni[2]
 put  3 into tlistasuoni[3]
  put  4 into tlistasuoni[4]
   put  5 into tlistasuoni[5]
    put  6 into tlistasuoni[6]
     put  7 into tlistasuoni[7]
      put  8 into tlistasuoni[8]
       put 1 into tlistasuoni[9]
        put  2 into tlistasuoni[10]
         put  3 into tlistasuoni[11]
          put  4 into tlistasuoni[12]
           put  5 into tlistasuoni[13]
            put  6 into tlistasuoni[14]
             put  7 into tlistasuoni[15]
              put  8 into tlistasuoni[16]
     
     i need to shuffle the elements of this array...and not the keys!!!..tlistasuoni

ok i have made a little code but this is wrong....can you help me??

Code: Select all


on mousedown

   repeat  until tlistasuoni[16] is not empty
      If tlistasuoni[x] is empty then
         If tlistasuoni[x] <> y then
            put  random(8) into y
            put y into tlistasuoni[x]
            add 1 to x
         end if
      end if
     end repeat
     end mousedown
     
     The result is this: 2416374676313355  ...this is bad...
     I need like this:    3254671834567128  thi is right...
   
Another code modified:

Code: Select all

on mousedown
   
   repeat  until tlistasuoni[16] is not empty
      If tlistasuoni[x] is empty then
         If tlistasuoni[x] <> y then
            put  random(8) into y
            put y into tlistasuoni[x]
            add 1 to x
         end if
      end if
      if  tlistasuoni[x] = Y then         ------------------little modify a little good but not right....
         put y into tlistasuoni[x]
      end if
      
   end repeat
   
   Result: 7518323486645281
Can some body help me???Best regards, Lestroso :oops:
Last edited by Fasasoftware on Thu Oct 25, 2018 4:39 pm, edited 2 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9660
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to create and Random Shuffle Array?

Post by dunbarx » Wed Oct 24, 2018 7:51 pm

Hi.

I am giving an basic solution to your question. "Basic" means there are no shortcuts or other shenanigans involved, so you can see a straightforward way to do this.

If you have nothing better to do, you might think about a way with only a single repeat loop. Hint: it will mean "combining" in some clever way the "put" lines in each of the two loops below.

In a button script, on a card with a field:

Code: Select all

on mouseUp
   repeat with y = 1 to 8
      put y into tlistasuoni[y]
   end repeat
     repeat with y = 9 to 16
      put y - 8 into tlistasuoni[y]
   end repeat
   combine tlistasuoni with return and comma
   sort tlistasuoni numeric by item 1 of each
   put tlistasuoni into fld 1
end mouseUp
Craig Newman

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Location: Italy
Contact:

Re: How to create and Random Shuffle Array?

Post by Fasasoftware » Wed Oct 24, 2018 9:32 pm

Dear Graig,
I thank you a lot....but your solution is not right for me....I need a random order of the elements of tlistasuoni....

I thank you again graig in every way....

Any other ideas???

Best regards,

Lestroso :oops:

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: How to create and Random Shuffle Array?

Post by SparkOut » Wed Oct 24, 2018 9:59 pm

I am not sure what exactly you are trying to achieve, but maybe this will produce what you want?

Code: Select all

put "1,2,3,4,5,6,7,8" into tList1
put tList1 into tList2
sort the items of tList1 by random(8)
sort the items of tList2 by random(8)
put comma & tList2 after tList1
repeat with tX = 1 to 16
   put item tX of tList1 into tListasuoni[(tX)]
end repeat

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9660
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to create and Random Shuffle Array?

Post by dunbarx » Thu Oct 25, 2018 3:25 am

Lestroso.

Hmmm.

I did not get that from your post, which had very ordered elements in the array

Anyway, try something like this:

Code: Select all

on mouseUp
  
   repeat with y = 1 to 16
      put y into line y of tList
   end repeat
   
   sort tList by random(9999)
   
   repeat with y = 1 to 16
    put line y of tList into  tlistasuoni[y]
   end repeat
   
   combine tlistasuoni with return and comma
   sort tlistasuoni numeric by item 1 of each
   put tlistasuoni into fld 1
end mouseUp
This method does not require that you write a clever handler that builds a random array on the fly.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9660
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How to create and Random Shuffle Array?

Post by dunbarx » Thu Oct 25, 2018 3:49 am

As long as we are on the subject, here is a clever handler:

Code: Select all

on mouseUp
   put "" into fld 1
   wait 22
   
   repeat with y = 1 to 16
      put y into line y of tList
   end repeat
   
   repeat with y = 1 to 16
      put any line of tList into temp
      put  temp into tlistasuoni[y]
      delete line lineOffset(temp,tList) of tList
   end  repeat
   
   combine tlistasuoni with return and comma
   sort tlistasuoni numeric by item 1 of each
   put tlistasuoni into fld 1
end mouseUp
It isn't really that clever, it just works in a different, and perhaps more direct way. Do you see how it is necessary to "find" the correct line to delete on each pass of the loop?

Craig

Fasasoftware
Posts: 203
Joined: Mon Oct 31, 2011 9:36 pm
Location: Italy
Contact:

Re: How to create and Random Shuffle Array?

Post by Fasasoftware » Thu Oct 25, 2018 4:36 pm

Dear friends!! i thank you very much!!! Thank you for your time dedicated to me and for your fast answer!!

I thank you a lot dear dunbarks(Craig).....than you again..

In truth Sparkout has solved in full the script that i need!!! Thank you again SPARKOUT!!!

In your code there was a little error....you add a "the" to items....here below the right code...:

Code: Select all

   on mouseup
   put "1,2,3,4,5,6,7,8" into tList1
   put tList1 into tList2
    sort items of tList1 by random(8)    ------before was.. sort "the items"...don't working
   sort items of tList2 by random(8) -----this work fine
   put comma & tList2 after tList1
   repeat with tX = 1 to 16
      put item tX of tList1 into tlistasuoni[(tX)]
   end repeat
   end mouseup

Thanks a lot again to every body!! best regards...Lestroso :D :D :D

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: (Solved!)How to create and Random Shuffle Array?

Post by jiml » Thu Oct 25, 2018 10:35 pm

In the spirit of "if it can be done, it can be overdone", here is a function that will create your randomized array for any number of digits (not just 8 ) and any number of repeats (not just 2).

Code: Select all

function randomizedArray, numberOfDigits, numberofRepeats
   repeat with D = 1 to numberOfDigits
      put D into line D of tList
   end repeat
   repeat with R = 1 to numberofRepeats
      do "put tList into tList" & R
      do "sort tlist" & R & " by random(numberOfDigits)"
      do "put tList" & R & "& cr after outList"
   end repeat
   delete char -1 of outList
   split outList by row
   return outList
end randomizedArray
Call it like so:

Code: Select all

randomizedArray(8,2)
for your example or

Code: Select all

randomizedArray(30,5)
:D
Jim Lambert

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”