Random numbers with no duplicates

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
fhs14647
Posts: 21
Joined: Sat May 16, 2020 11:40 am

Random numbers with no duplicates

Post by fhs14647 » Mon Feb 08, 2021 6:27 pm

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Random numbers with no duplicates

Post by bn » 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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Random numbers with no duplicates

Post by Klaus » 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

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Random numbers with no duplicates

Post by Thierry » 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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

fhs14647
Posts: 21
Joined: Sat May 16, 2020 11:40 am

Re: Random numbers with no duplicates

Post by fhs14647 » Mon Feb 08, 2021 10:56 pm

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

fhs14647
Posts: 21
Joined: Sat May 16, 2020 11:40 am

Re: Random numbers with no duplicates

Post by fhs14647 » Mon Feb 08, 2021 11:02 pm

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

fhs14647
Posts: 21
Joined: Sat May 16, 2020 11:40 am

Re: Random numbers with no duplicates

Post by fhs14647 » Mon Feb 08, 2021 11:04 pm

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!

kdjanz
Posts: 300
Joined: Fri Dec 09, 2011 12:12 pm

Re: Random numbers with no duplicates

Post by kdjanz » Mon Feb 08, 2021 11:36 pm

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.

Post Reply