Page 1 of 1

Random numbers with no duplicates

Posted: Mon Feb 08, 2021 6:27 pm
by fhs14647
Dear experts

I know that there are better ways to solve this problem, but I want to try it in a certain way to solve my problem

I want to
a.) create a list of 6 random numbers between 1 and 10
b.) these numbers should also be written in a list
c.) for every new random numbers one should look in the list for a duplicate
d.) if a duplicate is found, the duplicate item in the list is deleted, the repeat counter is subtracted with 1
e.) in the end the list is written to a text field

I am not very good in programming and LiveCode and I tried to solve it this way without success ;-)
Here is my coding attempt ... what is wrong?

Code: Select all

on mouseUp
   local x, myList, z, myNumber
   
   repeat with x = 1 to 6
      ## generate 6 numbers between 1 and 10
      put random(10) into myNumber
      ## put number to a list
      put myNumber into item x of myList
      ## every item in list in checked
      repeat for each item z in myList
         ## no check for the first number
         if x > 1 then
            if (item z of volleListe) = myNumber then
               ## Delete the duplicat
               delete item z of myList
               subtract 1 from x
               
            else
               ## no duplicate
               put item myNumber of myList & return after field "Field"
            end if
         end if
      end repeat
      
   end repeat

Thanks for your help!!
Mike

Re: Random numbers with no duplicates

Posted: Mon Feb 08, 2021 7:09 pm
by bn
Hi Mike,

something like this...

Code: Select all

on mouseUp
   repeat until the number of lines in tData is 6
      put random(10) into tTest
      if tTest is not among the lines of tData then
         put tTest & cr after tData
      end if
   end repeat
   delete char -1 of tData -- a return
   -- the list is not orderd
   
   -- order list
   -- sort tData ascending numeric -- unblock if you want the list to be ordered
   -- end order list
   
   put tData into field 1
end mouseUp
Kind regards
Bernd

Re: Random numbers with no duplicates

Posted: Mon Feb 08, 2021 7:19 pm
by Klaus
Hi Mike,

yep, don't think too complicated and let LC do the tedious stuff! :-)
Here another solution without the need to check for doublettes:

Code: Select all

on mouseUp 
  ## Create a list with 10 numbers
   repeat with i = 1 to 10
      put i into item i of tList
   end repeat

  ## Let LC mix the items in the list
   sort items of tList by random(1000)

   ## Throw away what you do not need:
   delete item 7 to 10 of tList

   ## Done
   put tList into fld "die zufälligen zahlen"
end mouseUp
Best

Klaus

Re: Random numbers with no duplicates

Posted: Mon Feb 08, 2021 8:46 pm
by Thierry
Thanks for your help!!
Hi Mike,

another one....

Code: Select all

function get6RandomNumbersFrom aList
   repeat 4
      delete any item of aList
   end repeat
   return aList
end get6RandomNumbersFrom

on mouseUp
   put get6RandomNumbersFrom("1,2,3,4,5,6,7,8,9,10") into fld 1
end mouseUp
Thierry

Re: Random numbers with no duplicates

Posted: Mon Feb 08, 2021 10:56 pm
by fhs14647
bn wrote:
Mon Feb 08, 2021 7:09 pm
Hi Mike,

something like this...

Code: Select all

on mouseUp
   repeat until the number of lines in tData is 6
      put random(10) into tTest
      if tTest is not among the lines of tData then
         put tTest & cr after tData
      end if
   end repeat
   delete char -1 of tData -- a return
   -- the list is not orderd
   
   -- order list
   -- sort tData ascending numeric -- unblock if you want the list to be ordered
   -- end order list
   
   put tData into field 1
end mouseUp
Kind regards
Bernd


Incredible small code, perfect!!
Thanks a lot!!

Re: Random numbers with no duplicates

Posted: Mon Feb 08, 2021 11:02 pm
by fhs14647
Thierry wrote:
Mon Feb 08, 2021 8:46 pm
Thanks for your help!!
Hi Mike,

another one....

Code: Select all

function get6RandomNumbersFrom aList
   repeat 4
      delete any item of aList
   end repeat
   return aList
end get6RandomNumbersFrom

on mouseUp
   put get6RandomNumbersFrom("1,2,3,4,5,6,7,8,9,10") into fld 1
end mouseUp
Thierry
Incredible solution, thanks a lot!!

Re: Random numbers with no duplicates

Posted: Mon Feb 08, 2021 11:04 pm
by fhs14647
Klaus wrote:
Mon Feb 08, 2021 7:19 pm
Hi Mike,

yep, don't think too complicated and let LC do the tedious stuff! :-)
Here another solution without the need to check for doublettes:

Code: Select all

on mouseUp 
  ## Create a list with 10 numbers
   repeat with i = 1 to 10
      put i into item i of tList
   end repeat

  ## Let LC mix the items in the list
   sort items of tList by random(1000)

   ## Throw away what you do not need:
   delete item 7 to 10 of tList

   ## Done
   put tList into fld "die zufälligen zahlen"
end mouseUp
Best

Klaus
Perfect, thanks a lot!

Re: Random numbers with no duplicates

Posted: Mon Feb 08, 2021 11:36 pm
by kdjanz
Please note that the sort random method also works with letters or words or y sort of items that you can put in a list. In one stack I have a bunch of points (integer pairs) and in another I have grid references (G1,R4,Z10) and I use the sort list random(1000) method to mix things up without dupes.

Very useful & flexible technique to have in the toolbox, and I learned it here on the forums! Thanks for all the help over the years folks.