How to random a number that will not repeat itself?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
royF
Posts: 13
Joined: Mon Oct 10, 2022 10:48 pm

How to random a number that will not repeat itself?

Post by royF » Tue Oct 11, 2022 8:37 pm

How to random a number from 1 to 10 for example...
but the generate number will not repeat itself!

Thanks!!

I try this simple code....

Code: Select all

on Randint
   put random (70) into generate_number
   if generate_number is in fld "Old Numbers"
   then
      mouseUp
   else
      put generate_number&" " after the last char in fld "Old Numbers"
   end if
   answer generate_number, cr,cr, fld "Old Numbers" 
end Randint
but Livecode blocked it because it caused software overload.
"Randint has reached the recursion limit of: 400000. Execution will be terminated to prevent hung."
Last edited by royF on Tue Oct 11, 2022 9:03 pm, edited 1 time in total.

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

Re: How to random a number that will not repeat itself?

Post by dunbarx » Tue Oct 11, 2022 8:50 pm

Hi.

Many fun ways to do this. Make a field and a button on a new card. Put the digits from 0 to 9 in separate lines in the field.

Put this in the button script:

Code: Select all

on mouseUp
   put fld 1 into source
   repeat the number of lines of source
      put random(the number of lines of source) into currentLine
      put line currentLine of source & return after accum
      delete line currentLine of source
   end repeat
   answer accum
end mouseUp
Craig

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

Re: How to random a number that will not repeat itself?

Post by dunbarx » Tue Oct 11, 2022 8:51 pm

I made the handler above verbose, just so you can see what is going on. Step through it if you like.

Craig

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

Re: How to random a number that will not repeat itself?

Post by dunbarx » Tue Oct 11, 2022 8:54 pm

Rereading your post, is what I offered what you were asking for?

It is simple to pull a random number from a list, but when you asked that the results not repeat, I assumed you wanted to pull ALL those numbers, but without repeating any of them. That is why we have to delete our random choices once we have made them, so they are not around to be chosen again.

Craig

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

Re: How to random a number that will not repeat itself?

Post by dunbarx » Tue Oct 11, 2022 8:57 pm

Feeling like Richmond again...

Here is a more sophisticated way to do what you want. Let me know if you want to talk about how it works:

Code: Select all

on mouseUp
   get fld 1
   sort it by random(9999)
   answer it
end mouseUp
Craig

royF
Posts: 13
Joined: Mon Oct 10, 2022 10:48 pm

Re: How to random a number that will not repeat itself?

Post by royF » Tue Oct 11, 2022 10:10 pm

You are a king👑!!!
I learned a lot from you and you made me think creatively. :D
I would love to talk about how it works! (The last one)

Code: Select all


on openCard  ----> Card "mainCard"
	randomPickNumber 	
end openCard

on mouseUp  ----> BUTTON "RANDOM A NUMBR"
	add 1 to counter
	put counter line of random_list_num Into generate_number
	answer generate_number
end mouseUp


on randomPickNumber ----> Card "mainCard"

   put empty into random_list_num
   
   repeat with Numb = 1 to 70
      put Numb & return after random_list_num
   end repeat
   
   get random_list_num
   sort it by random(9999)
   put it into random_list_num 
    
end randomPickNumber


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

Re: How to random a number that will not repeat itself?

Post by dunbarx » Wed Oct 12, 2022 3:56 am

Hi.

I see you are going to have fun with LiveCode. Good. A couple of observations:

It is easy to think that with LiveCode if you write a reasonable English sentence LC will make sense of it. This is a tribute to how English-Like the language is. But it is still a language, with rules of syntax that must be observed. So this:

Code: Select all

add 1 to counter
	put counter line of random_list_num Into generate_number
comes out as:

Code: Select all

put 1 line of random_list_num Into generate_number
Besides being unable to compile, did you mean one line? Did you mean line 1? But you tried and that is worthwhile. You will fine tune this as you go forward.

This:

Code: Select all

 repeat with Numb = 1 to 70
      put Numb & return after random_list_num
   end repeat
might be made just a bit more elegant as:

Code: Select all

 repeat with Numb = 1 to 70
      put Numb into line Numb of random_list_num
   end repeat
I just find that better. But yours works just fine. You do not need to empty the variable random_list_num. LC will happily create it at first mention in the repeat loop. And once you have already built the variable, there is no need to put it into another. Just work it directly:

Code: Select all

on randomPickNumber
   repeat with Numb = 1 to 70
    put Numb into line Numb of random_list_num
   end repeat
   
   sort random_list_num by random(9999)
end randomPickNumber
Keep at it. Keep posting.

Craig

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”