# Thread: Random numbers and strings

1. ## Random numbers and strings

I have to make random generator for numbers and strings, in SilkTest, without using RandStr, RandInt and RandSeed (iSeed) methods. I need to write my own method, algorithm...
Can someone help me, please? [img]/images/graemlins/confused.gif[/img]

2. ## Re: Random numbers and strings

I dont understand what's the point of not using RandStr, RandInt etc for creating random values. Be specific in the requirement and explain the constraints which prevents you to use in built functions.

3. ## Re: Random numbers and strings

[ QUOTE ]
I dont understand what's the point of not using RandStr, RandInt etc for creating random values. Be specific in the requirement and explain the constraints which prevents you to use in built functions.

[/ QUOTE ]

Maybe Amy has the same stamp that some of our developers here have (similar to that from Sharper Image stamp) which says "Re-invented here." [img]/images/graemlins/shocked.gif[/img]

4. ## Re: Random numbers and strings

If you can not use SilkTest functions, you have to either write your own
http://en.wikipedia.org/wiki/Linear_...tial_generator

or use some random functions from win32 API dll's

5. ## Re: Random numbers and strings

Thanks guys.
I have started to write my own function. I have used Microsec for iSeed and then I put it in relation
iRandom=(65539*iSeed) % (2**31)

I made function to return exact length number(it works only for 2-9 digit numbers, that is problem too).

But, problem is when I call function from main, for example 3 times I receive 3 random numbers, but second and third are same.
main()
int iN=GetRandomNumber(9)
Print(iN)
iN=GetRandomNumber(9)
Print(iN)
iN=GetRandomNumber(9)
Print(iN)
output:
419615009
178104289
178104289

Here is code, it says more than my explanation, and you can try it. [img]/images/graemlins/smile.gif[/img]

integer GetRandomNumber(integer iLength)

integer i,j,iSeed, iNum,iRan
array[100] of integer aV
iSeed=0
iRan=0
iNum=0

iSeed = GetDateTimePart (GetDateTime (), DTP_MICROSEC)

iRan=Abs((65539*iSeed) % (2**31))

string sRange = "8"
string sStart = "1"
i=1

while i&lt;iLength
sRange=sRange+"9"
sStart=sStart+"0"
i=i+1

iNum=iRan%Val(sRange)+Val(sStart)
return iNum

6. ## Re: Random numbers and strings

Erm, GetDateTime() is not actually returning microseconds, its precision is tens of miliseconds at best (0.01), maybe even worse. So your call tree times in a row is using the same starting value.
The real problem is, you totaly missed that using LCG is an iterative process, so for the next random value you use the previous one. Something like

integer iRan = -1 //global variable
integer GetRandomNumber(integer iLength)
...
if(iRan==-1) iRan=iSeed
iRan=Abs((iRan*65539) % (2**31))
...

and I would suggest to check that iLength &lt; 10, because you are not going to produce greater value this way...

7. ## Re: Random numbers and strings

...and try to sanitize your code before posting, j and aV variables are not used in the script

8. ## Re: Random numbers and strings

Yes, I have tried LCG as iterative process, in first place and I make array of 100 random numbers. The longest length of random number is 2**31.
I don't have idea how to get one random number with precise length from that array?

code:
array[100] of integer aV
iSeed = GetDateTimePart (GetDateTime (), DTP_MICROSEC)
aV[1]=iSeed

for j=1 to 99
aV[j+1]=(((2**61)*aV[j]) % (2**32))
Print("aV[{j+1}]= "+'{Abs(aV[j+1])}')

9. ## Re: Random numbers and strings

You can not get a precise length. You can get just the random numbers and use them to produce what you need.
Your algorithm is not so bad, just use it with the iRan as global variable.
Of course it all depends on the purpose of those random numbers. Their quality is not high. What you need them for?

10. ## Re: Random numbers and strings

They need me for making User IDs, Names, Passwords etc, for some app. testing (Adding, deleting users to/from system....etc...)

Page 1 of 2 12 Last

#### 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.