Page 1 of 1

Sorting Arrays numericly

Posted: Thu Oct 25, 2012 2:27 pm
by Pincer
OK after much google searching and testing ideas I still can't fix my problem, hoping someone here can help.

I am trying to create a lottery program for my advanced higher computing projecting livecode, it generates 6 random numbers from 1 to 49 then gives the user 2 chocies to choose his own numbers or have the computer generate numbers for him. I have it all well and working apart from 2 problems. Firstly it has repeating numbers sometimes, I have tried using another array of possible numbers it choosing a random number from that then deleteing that number the code for it is as follows

Code: Select all

 on Generate_users_numbers
      local usersnumbers, possiblenumbers, numberoflines, arrayfile, arraypossiblenumbers
   put url ("file:" & "C:/Users/Pincer/Desktop/Computing/Project/a.txt") into possiblenumbers
   put the number of lines in possiblenumbers into numberoflines
   repeat with loop = 1 to numberoflines
      put line loop of possiblenumbers into arrayfile[loop]
   end repeat
   repeat with loop = 1 to 7
      put random(49) into arraypossiblenumbers
      put arrayfile[arraypossiblenumbers] into usersnumbers
      repeat until usersnumbers is not empty then
       put random(49) into arraypossiblenumbers
      put arrayfile[arraypossiblenumbers] into usersnumbers
      end repeat
      put usersnumbers into array_users_numbers[loop]
      delete variable arrayfile[arrayposiblenumbers]
   end repeat
end Generate_users_numbers
The second problem I have is it won't sort either of the arrays with the numbers properly, the closest i could get was a piece of code I got off a website that does sort it but sorts on only the first number, e.g. if I enter say 2,37,8,9,12,24 it should come up with 2,8,9,12,24,37 but instead it comes up with 12,2,24,37,8,9.

Please can someone help me.

Re: Sorting Arrays numericly

Posted: Thu Oct 25, 2012 3:02 pm
by Thierry
Hi,

Arrays are strange beast :)

You can't rely on any order; that's the way it is.

For sorting them, the idea is to get the keys of the array, which gives you a list
then sort this list, and finally do a loop ( repeat ) on the sorted list of keys
and get the value of each key....

Sorry, no time to code this but hope you get the idea

HTH

Thierry

Re: Sorting Arrays numericly

Posted: Fri Oct 26, 2012 1:35 am
by Bernard
I think this solves both of your problems.

Code: Select all

on mouseUp
   local x,r
   repeat while the number of items in x < 7
      put random(49) into r
      if r is among the items of x then 
         next repeat
      else
         put r & comma after x
      end if
   end repeat
   delete char -1 of x
   sort items of x ascending numeric
   put x into msg
end mouseUp

Re: Sorting Arrays numericly

Posted: Fri Oct 26, 2012 12:10 pm
by Pincer
Thanks will try it now.

Re: Sorting Arrays numericly

Posted: Fri Oct 26, 2012 1:41 pm
by Klaus
Another approach, which avoids "repeat until...", that always scares me somehow 8)

This is just like you would do it manually with a sheet of paper and a pencil, think about it :D

Code: Select all

on mouseUp

  ## Write down all number from 1 to 49
  repeat with i = 1 to 49
    put i & CR after tList
  end repeat

  ## Delete last CR
  delete char -1 of tList

  ## Now DRAW six random numbers
  repeat 6
    put random(the num of lines of tList) into tRand
    put line tRand of tList & CR after tRandList

    ## Wipe out the line you just DRAW
    delete line tRand of tList
  end repeat
  delete char -1 of tRandList
  sort tRandlist numeric
  put tRandList into fld 1
end mouseUp
Best

Klaus

Re: Sorting Arrays numericly

Posted: Sun Oct 28, 2012 5:23 pm
by Randy Hengst
How about this... similar to what Klauss suggested

on generateLotteryNumbers firstNumber,lastNumber
local tTheNumbers
put empty into tTheNumbers
repeat with theNumber = firstNumber to lastNumber
-- create the list of lottery numbers -- this way there will be no repeated numbers
put theNumber & "," after tTheNumbers
end repeat
delete last char of tTheNumbers -- must remove trailing comma

sort items of tTheNumbers by random(10000)
put item 1 to 6 of tTheNumbers into tTheNumbers
sort numeric items of tTheNumbers ascending
put tTheNumbers into field "LotteryNumbers"
end generateLotteryNumbers
on mouseUp
generateLotteryNumbers 1,49
end mouseUp

be well,
randy

Re: Sorting Arrays numericly

Posted: Tue Jan 15, 2013 3:05 pm
by Pincer
Sorry I took so long to get back to you guys, been busy with all my subjects and projects. Finaly got my program fully working. In the end went with Bernard's idea. Thanks everyone who posted for the help.