Thread: Alphanumeric random generation in QTP

1. Alphanumeric random generation in QTP

How do you go about randomly generating strings from alphanumeric characters in QTP?

So far, the only functions I've seen are all doing random number generations.

Thanks.

2. Re: Alphanumeric random generation in QTP

I dont know if there is any logic to do that but based on your random number generated u can append the character associated with its ascii code.
x = RandomNumber.Value(0,100)
Msgbox x
Msgbox Chr(x)
Msgbox x&amp;Chr(x)
Something on these lines. You can get an appended string having numeric / special symbols / alphanumeric characters in the return string

3. Re: Alphanumeric random generation in QTP

@Shreejit - Amazing piece of logic there!..

@SilkInTheCity - if you are specific for "generating strings from alphanumeric characters" i have a code piece for you that take the passwd length and the permitted chars and sets your randomised string as required. This way i have the control over the string length and i get my alphanumeric combination as well...hope this helps!

permittedChars = "abcdefABCDEF 012345"

pwd = ""
pwd = pwd &amp; mid(permittedChars,int(rnd*len(permittedChars)+1), 1) ' might have got the +1 in the wrong place there
next

Regards,
Ravi Salunkhe.
(Worker bees can leave,
Even drones can fly away,
The queen is their slave!)

4. Re: Alphanumeric random generation in QTP

'Generate 1 string with 10 random character
Dim tenstr
For i = 1 to 10
n = RandomNumbers(64,90)
tenstr = tenstr &amp; chr(n)
Next

5. Re: Alphanumeric random generation in QTP

Here's how I roll, I create a random number and then replace the numbers with text

'This function returns a unique number as string
Function uniqueNumber()
uniqueNumber = DatePart("yyyy", Now) &amp; DatePart("y", Now) &amp; DatePart("h", Now) &amp; DatePart("n", Now) &amp; DatePart("s", Now)
End Function

'This function returns a unique string
Function uniqueText()
num = uniqueNumber()
num = Replace(num, "0", "a")
num = Replace(num, "1", "b")
num = Replace(num, "2", "c")
num = Replace(num, "3", "d")
num = Replace(num, "4", "e")
num = Replace(num, "5", "f")
num = Replace(num, "6", "g")
num = Replace(num, "7", "h")
num = Replace(num, "8", "i")
num = Replace(num, "9", "j")
uniqueText = num
End Function

6. Re: Alphanumeric random generation in QTP

There are several solutions here already but, in case you're interested in seeing alternatives, here is how I did it.

<font class="small">Code:</font><hr /><pre>

arrCharacters = Array("a","b","c","d","e","f","g","h","i","j","k", "l","m","n","o","p","q","r","s","t","u","v","w","x ","y","z","A","B","C","D","E","F","G","H","I","J", "K","L","M","N","O","P","Q","R","S","T","U","V","W ","X","Y","Z","0","1","2","3","4","5","6","7","8", "9")

' 10 digit string
iStringLength = 10

for i = 1 to iStringLength
strRandomString = strRandomString &amp; ChooseRandomElement(arrCharacters)
next

msgbox strRandomString

'************************************************* ************************************************** ************************************************** *************************
'***
'*** Function Name: ChooseRandomElement
'*** Description: Choose a random element from an array and return it
'*** Author: Dennis Belew
'*** Date Written: 06/17/2010
'*** Inputs: elemetArray -- an array of values
'*** Dependencies: RandomNumberInRange function
'*** Outputs: N/A
'*** Return Codes: N/A
'***
'************************************************* ************************************************** ************************************************** *************************
Public function ChooseRandomElement(elementArray)
ChooseRandomElement = elementArray(RandomNumberInRange(lbound(elementArr ay), ubound(elementArray)))
End Function

'************************************************* ************************************************** ************************************************** *************************
'***
'*** Function Name: RandomNumberInRange
'*** Description: Choose a random number between the minValue and maxValue parameters
'*** Author: Dennis Belew
'*** Date Written: 06/17/2010
'*** Inputs: minValue -- the smallest value returned
' maxValue -- the greatest value returned
'*** Outputs: N/A
'*** Return Codes: N/A
'***
'************************************************* ************************************************** ************************************************** *************************
Public function RandomNumberInRange(minValue, maxValue)
' Generate random value between minValue and maxValue.
randomize
valueRange = maxValue-minValue + 1
RandomNumberInRange = Int((valueRange * Rnd) + 1) - 1
RandomNumberInRange = RandomNumberInRange + minValue
End Function
</pre><hr />

7. Re: Alphanumeric random generation in QTP

Great idea..... Simple solution....

8. Re: Alphanumeric random generation in QTP

I prefer to use the following as its small, simple and flexible

Call the function passing the length of the required string:

MsgBox RandomAlphaNumeric(8)

MsgBox RandomAlphaNumeric(44)

Function RandomAlphaNumeric(vLth)

vNumLim = 36

For x = 1 To vLth
Randomize
vRandChar = Int((vNumLim * Rnd) + 1)
If vRandChar &gt; 26 Then
vTempString = vTempString &amp; Chr((vRandChar - 26) + 47)
Else
vTempString = vTempString &amp; Chr(vRandChar + 64)
End If
Next

RandomAlphaNumeric = vTempString

End Function

Mark Smith.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.