forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Random Name

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Visual DialogScript 6 Source Code
View previous topic :: View next topic  
Author Message
LiquidCode
Moderator Team


Joined: 05 Dec 2000
Posts: 1711
Location: Space and Time

PostPosted: Thu Feb 11, 2010 11:37 pm    Post subject: Random Name Reply with quote

This code creates a random string of letters. Good for creating random file names.

Useage: @RandomName(<Number of chars>)
Default is 4 id <Number of Chars> is left blank.

Code:

#Define function,RandomName
:RandomName
  #%1 = num of chars, blank for default of 4
  %c = "abcdefghijklmnopqrstuvwxyz"
  if @not(%1)
     %1 = 4
  end
  RANDOM @datetime(shn)
    %i = 1
    repeat
      %p = %p@substr(%c,@random(1,@len(%c)))
      %i = @succ(%i)
    until @equal(%i,@succ(%1))
  exit @upper(%p)

_________________
Chris
Http://vdsfreak.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
uvedese
Contributor
Contributor


Joined: 21 Jan 2006
Posts: 159
Location: Spain

PostPosted: Fri Feb 12, 2010 4:43 pm    Post subject: Reply with quote

Hi LiquidCode:

Very useful code. It can add an formatting argument, this way:

Code:
  #Define function,RandomName
:RandomName
  #%1 = num of chars, blank for default of 4
  #%2 = format string where N=number and L=letter; letter for default
  %c = "abcdefghijklmnopqrstuvwxyz"
  if @not(%1)
     %1 = 4
  end

  RANDOM @datetime(shn)
  %i = 1
  if @greater(%1,@len(%2))
    %2 = %2@fill(@diff(%1,@len(%2)),L)
  end
   
  repeat
    if @equal(@substr(%2,%i),N)
      %p = %p@random(0,9)
    else
      %p = %p@substr(%c,@random(1,@len(%c)))
    end
 
    %i = @succ(%i)
  until @equal(%i,@succ(%1))
exit @upper(%p)


We can define the format of the string:

Code:
%a = @randomname(8,NNLLLLLL)

___________

uVeDeSe
___________
Back to top
View user's profile Send private message Visit poster's website
SnarlingSheep
Professional Member
Professional Member


Joined: 13 Mar 2001
Posts: 759
Location: Michigan

PostPosted: Fri Feb 12, 2010 5:53 pm    Post subject: Reply with quote

Option for either <Length> or <Format String> and added characters:
*Edited to remove invalid filename characters.
Code:

#Define function,RandomName

info @RandomName(6)
info @RandomName(NNLLNNCCC)

exit

:RandomName
  #%1 = num of chars, blank for default of 4 or format string where N=number and L=letter; letter for default
  %c = "abcdefghijklmnopqrstuvwxyz"
  %d = "~!@#$%^&()_+`-={}[];',."
  if @not(%1)
     %1 = 4
  end

  RANDOM @datetime(shn)
  %i = 1
  if @not(@numeric(%1))
    %2 = %1
    %1 = @len(%1)
  end
   
  repeat
    if @equal(@substr(%2,%i),N)
      %p = %p@random(0,9)
    else
      if @equal(@substr(%2,%i),C)
        %p = %p@substr(%d,@random(1,@len(%d)))
      else
        %p = %p@substr(%c,@random(1,@len(%c)))
      end
    end

_________________
-Sheep
My pockets hurt...


Last edited by SnarlingSheep on Sat Feb 13, 2010 2:20 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
uvedese
Contributor
Contributor


Joined: 21 Jan 2006
Posts: 159
Location: Spain

PostPosted: Fri Feb 12, 2010 6:43 pm    Post subject: Reply with quote

If it wants to generate a consistent string as a valid file name, it must eliminate some characters:

Code:
%d = "~!@#$%^&()_+`-={}[];',."


___________

uVeDeSe
___________
Back to top
View user's profile Send private message Visit poster's website
LiquidCode
Moderator Team


Joined: 05 Dec 2000
Posts: 1711
Location: Space and Time

PostPosted: Tue Apr 26, 2011 4:11 pm    Post subject: Reply with quote

I revisited this code. I needed to generate many random names in more than a second and since the seed is based on the time it only changes every second. So I take it farther and also add a Random_Seed variable that is generated from the previous seed. So in theory, no two names should be the same when generated in less than a second.

Code:

#Define function,RandomName

#info @RandomName(6)
list create,1
%x = 0
repeat
     list add,1,@RandomName(NNLLNN)
     %x = @succ(%x)
until @equal(%x,10)
info @text(1)
exit

:RandomName
  #%1 = num of chars, blank for default of 4 or format string where N=number and L=letter; letter for default
  %c = "abcdefghijklmnopqrstuvwxyz"
  %d = "~!@#$%^&()_+`-={}[];',."
  if @not(%1)
     %1 = 4
  end
  if %%Random_seed
     Random %%Random_Seed
  else
     RANDOM @datetime(shn)@random(0,9)
  end
  %i = 1
  if @not(@numeric(%1))
    %2 = %1
    %1 = @len(%1)
  end

  repeat
    if @equal(@substr(%2,%i),N)
      %p = %p@random(0,9)
    else
      if @equal(@substr(%2,%i),C)
        %p = %p@substr(%d,@random(1,@len(%d)))
      else
        %p = %p@substr(%c,@random(1,@len(%c)))
      end
    end
    %i = @succ(%i)
  until @equal(%i,%1)
  %%Random_Seed = @random(1000)
 exit @upper(%p)

_________________
Chris
Http://vdsfreak.com
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> Visual DialogScript 6 Source Code All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum




Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group