How to generate a random string of a fixed length in cache
Is it possible to generate a random string in cache please advice thank you in advance
Discussion (4)1
Comments
Use ##class(%PopulateUtils).StringMin(minlen,maxlen)
Isn't it easier to use the built function StringMin, especially when its the code does exactly the same thing?
USER><FONT COLOR="#0000ff">w $length</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">%PopulateUtils</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">StringMin</FONT><FONT COLOR="#000000">(100,100))</FONT> 100Source code:
<FONT COLOR="#000080">ClassMethod </FONT><FONT COLOR="#000000">StringMin(
</FONT><FONT COLOR="#ff00ff">minlen </FONT><FONT COLOR="#000080">As %Integer </FONT><FONT COLOR="#000000">= </FONT><FONT COLOR="#000080">1</FONT><FONT COLOR="#000000">,
</FONT><FONT COLOR="#ff00ff">maxlen </FONT><FONT COLOR="#000080">As %Integer </FONT><FONT COLOR="#000000">= </FONT><FONT COLOR="#000080">1</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#000080">As %String </FONT><FONT COLOR="#000000">[ </FONT><FONT COLOR="#000080">ProcedureBlock </FONT><FONT COLOR="#000000">= 1 ]
{
</FONT><FONT COLOR="#0000ff">if </FONT><FONT COLOR="#800000">maxlen </FONT><FONT COLOR="#000000">'< </FONT><FONT COLOR="#800000">minlen </FONT><FONT COLOR="#800080">{
</FONT><FONT COLOR="#0000ff">set </FONT><FONT COLOR="#800000">len</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#0000ff">$$$PRand</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">maxlen</FONT><FONT COLOR="#000000">-</FONT><FONT COLOR="#800000">minlen</FONT><FONT COLOR="#000000">+1)+</FONT><FONT COLOR="#800000">minlen</FONT><FONT COLOR="#000000">,
</FONT><FONT COLOR="#800000">string</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#008000">""
</FONT><FONT COLOR="#0000ff">for </FONT><FONT COLOR="#800000">i</FONT><FONT COLOR="#000000">=1:1:</FONT><FONT COLOR="#800000">len </FONT><FONT COLOR="#800080">{
</FONT><FONT COLOR="#0000ff">Set </FONT><FONT COLOR="#800000">charn</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#0000ff">$s</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$$$PRand</FONT><FONT COLOR="#000000">(2):</FONT><FONT COLOR="#0000ff">$$$PRand</FONT><FONT COLOR="#000000">(26)+65,1:</FONT><FONT COLOR="#0000ff">$$$PRand</FONT><FONT COLOR="#000000">(26)+97),
</FONT><FONT COLOR="#800000">string</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#800000">string</FONT><FONT COLOR="#000000">_</FONT><FONT COLOR="#0000ff">$s</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">charn</FONT><FONT COLOR="#000000"><123:</FONT><FONT COLOR="#0000ff">$c</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">charn</FONT><FONT COLOR="#000000">),1:</FONT><FONT COLOR="#008000">" "</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#800080">}
</FONT><FONT COLOR="#0000ff">quit </FONT><FONT COLOR="#800000">string
</FONT><FONT COLOR="#800080">} </FONT><FONT COLOR="#0000ff">else </FONT><FONT COLOR="#800080">{ </FONT><FONT COLOR="#0000ff">quit </FONT><FONT COLOR="#008000">"" </FONT><FONT COLOR="#800080">}
</FONT><FONT COLOR="#000000">}</FONT>It depends on, how long this string should be, and what do you expect to see there.
The simplest way is just generating it with $random.
set string="" set length=100 for i=1:1:length set string=string_$char($random(26)+97)
To many time later, anyway for keep reference
Set string = ""Set length = 256For i=1:1:length {
Set Up = $random(2)
Set tNum = $random(28)
If (tNum = 26) {
Set tChar = "-"
}elseif (tNum = 27) {
Set tChar = "_"
} Else {
If Up {
// Upper case rangeSet tChar = $CHAR(tNum+65)
} Else {
// Lower case rangeSet tChar = $CHAR(tNum+97)
}
}
Set string = string _ tChar
}