Snippet Generate Random Sting - GenString

Reaction score
341
Heres a simple code i wrote that will generate a string. I have no idea what it could be used for but i was bored. Anyways it takes 1 argument Length. If you havent guessed , It will make the length of the string that long.

Code:
JASS:
function GenString takes integer length returns nothing
    local string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    local string array random[length]
    local string GS = ""
    local integer i = 0
    local integer RI
    loop
        exitwhen i == length
        set RI = GetRandomInt(1,52)
        set random<i> = SubStringBJ(alpha,RI,RI)
        set GS = GS + random<i>
        set i = i + 1
    endloop
    call DisplayTextToForce( GetPlayersAll(), &quot;                                 &quot; + GS)
endfunction</i></i>


Example use :
Code:
Generate
    Events
        Player - Player 1 (Red) types a chat message containing -gen as An exact match
    Conditions
    Actions
        Custom script:   call GenString(GetRandomInt(5,20))

Screenshot ( idk why....)
gen.jpg
 

Cheesy

some fucker
Reaction score
95
Nice function, but I don't see how this could be useful. :rolleyes:

Still good nonetheless. +Rep

EDIT: Given out too much in the past 24 hours, I'll give some tomorrow.
 

DrEvil

FCRI Associate!
Reaction score
111
Code:
JASS:
function GenString takes integer length returns nothing
    local string alpha = &quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;
    local string array random[length]
    local string GS = &quot;&quot;
    local integer i = 0
    local integer RI
    loop
        exitwhen i == length
        set RI = GetRandomInt(1,52)
        set random<i> = SubStringBJ(alpha,RI,RI)
        set GS = GS + random<i>
        set i = i + 1
    endloop
    call DisplayTextToForce( GetPlayersAll(), &quot;                                 &quot; + GS)
endfunction</i></i>


Example use :
Code:
Generate
    Events
        Player - Player 1 (Red) types a chat message containing -gen as An exact match
    Conditions
    Actions
        Custom script:   call GenString(GetRandomInt(5,20))



How does the custom script get the random letters ?

And how does it know how many numbers to count , there is no number for length ?

Theres no functions in that part , apart from the custom script ,
Please help me understand .

Thanks DrEvil ^^
 

saw792

Is known to say things. That is all.
Reaction score
280
JASS:
function GenString takes integer length returns nothing


Takes integer length. As in, the user specifies the length of the string.

JASS:
set RI = GetRandomInt(1,52)


Picks a random integer, which corresponds to a letter in the character string at the top of the function.

In the GUI Trigger (the one that has the custom script action) it calls a function, in this case the function that he has posted. Calling a function executes the functions actions, causing it to work.
 

Flare

Stops copies me!
Reaction score
662
1) You should really return the string, as people may want to use it for something other than displaying it on the screen - with what you've got there, there's a <1% chance* that you'll see the same string again (since the odds of getting a particular string are 1 in 52^length), and the fact that you have no reference. If you go by your example, how are you supposed to determine that a player typed the correct message, since all you did was display it onscreen, without any specific, accessible reference

*Assuming that length > 1, since if length was 1, you'd have about a 2% chance of getting the same string

2) Why are you indicating a size for the array? There's no need, just leave it as
JASS:

(actually, you don't even need that, you could simply do)
JASS:
set GS = GS + SubString (0, 51) //I think the native substring function works from 0 to ((string length) - 1)


Also, the option to specify the source string might make this a bit more useful (and a default string if the source string was blank i.e. you would get "abc..." if you entered "" for the parameter) and the option to allow repeat characters
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top