Generating unique random string

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
sc12
Posts: 10
Joined: Mon Jan 11, 2021 2:04 am

Generating unique random string

Post by sc12 » Wed Jan 20, 2021 8:18 am

Hi!

Does anyone have any idea on how to generate a random unique code or string? Like how base64Encode returns a text string.

Thanks in advance!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9840
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Generating unique random string

Post by FourthWorld » Wed Jan 20, 2021 8:38 am

The uuid function may be just what you're looking for.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

sc12
Posts: 10
Joined: Mon Jan 11, 2021 2:04 am

Re: Generating unique random string

Post by sc12 » Wed Jan 20, 2021 9:08 am

Ok, will try it out, thanks a lot!

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Generating unique random string

Post by mrcoollion » Wed Jan 20, 2021 9:30 am

For fun I made a little random string routine that gives some control on what you want to have in the random string (printable char only) and how long you want the string to be. Hope that it is of some use for anyone.

Regards,

Paul

Code: Select all

on mouseUp pButtonNumber
   -- See url: https://theasciicode.com.ar
   --Printable chars	From nbr	To nbr	
   --1 Special Chars 1	32	47	(32 is space)
   --2 Numbers 	48	57	(48 is 0)
   --3 Special Chars 2	58	64	(58 is : and 64 is  @)
   --4 Caps A-Z	65	90	
   --5 Letter Chars	91	96	
   --6 Letters a-z	97	122	
   --7 Special Chars 3	123	126	( 123 is [ and 126 is ~)
   put 7 into InLength
   put(1234567) into InSelection
   put MakeRandomString (InLength,InSelection) into tRandomString
   answer tRandomString
end mouseUp


Function MakeRandomString InLength,InSelection
   -- See url: https://theasciicode.com.ar
   --Printable chars	From nbr	To nbr	
   --1 Special Chars 1	32	47	(32 is space)
   --2 Numbers 	        48	57	(48 is 0)
   --3 Special Chars 2	58	64	(58 is : and 64 is  @)
   --4 Caps A-Z	        65	90	
   --5 Letter Chars	91	96	
   --6 Letters a-z	97	122	
   --7 Special Chars 3	123	126	( 123 is [ and 126 is ~)
   ----------------------------------------------------------------
   -- Pre Error check
   If InLength is not a number then return "Error 1"
   If InSelection is not a number then return "Error 2"
   if InLength > 50 then return "Error 3" 
   //Build string of ascii codes.
   put "32,47" into aCharsAllowedChoice[1]
   put "48,57" into aCharsAllowedChoice[2]
   put "58,64" into aCharsAllowedChoice[3]
   put "65,90" into aCharsAllowedChoice[4]
   put "91,96" into aCharsAllowedChoice[5]
   put "97,122" into aCharsAllowedChoice[6]
   put "123,126" into aCharsAllowedChoice[7]
   -- Fill allowed chars string
   put "" into tCharsString // Just to make sure the variable is empty, should not be neccesary
   put ""into tErrorCheck // Just to make sure the variable is empty, should not be neccesary
   repeat with tChoice = 1 to 7
      put tChoice after tErrorCheck
      if InSelection contains tChoice
      then
         repeat with tNbr = item 1 of aCharsAllowedChoice[tChoice]  to item 2 of aCharsAllowedChoice[tChoice]
            if tCharsString is "" then put numToChar(tNbr) into tCharsString else put tab & numToChar(tNbr) after tCharsString
         end repeat
      end if
   end repeat 
   -- Fill Random string
   set itemdelimiter to tab
   put "" into tRandomCharString
   repeat with tlengthNbr = 1 to InLength
      if tRandomCharString is "" then put any item of tCharsString into tRandomCharString
      else
         put any item of tCharsString after tRandomCharString
      end if
   end repeat
   return tRandomCharString
end MakeRandomString

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”