1. ## a simple question about randomnumber

Hi,

Now I hava a simple question of my code sector as below, I want to get a random number by QTP, but no result was returned back. I wonder whether there is some problems within my code. Could you please review it and locate the problem for me?

The code sector I mentioned is as below:

Dim rndnumber

function rand(k,n)
n=int((k-n+1)*rnd+n)
end function

rndnumber=rand(100,1)
msgbox rndnumber

Again, thank you for your help!

2. ## Re: a simple question about randomnumber

hello friend,

One suggestion please change the font color for your thread it is not at all visible in the forum.

Thanks
testking123

3. ## Re: a simple question about randomnumber

[ QUOTE ]
hello friend,

One suggestion please change the font color for your thread it is not at all visible in the forum.

Thanks
testking123

[/ QUOTE ]

4. ## Re: a simple question about randomnumber

hai friend,

Try this i got this code while surfing it may be useful to you.

dim number
number = (1000000-100000) * rnd(1) + 100000

while invalidnumber(number)
number = (1000000-100000) * rnd(1) + 100000
wend

msgbox int(number)

function invalidnumber(nbr)
if number &lt; 500000 then
invalidnumber = true
exit function
end if

invalidnumber = false
end function

Thanks
testking123

5. ## Re: a simple question about randomnumber

Why not use QTP's inbuilt random number generator:

x=RandomNumber (0,100)

This will generate a random number between 0 &amp; 100

Mark Smith.

6. ## Re: a simple question about randomnumber

thanks a lot&amp;#65292;but the results are always "734992", it's not a random number.
thank you!
for this problem, i have found another way to deal with, The code sector is as below:

rand()
int n
n=randomnumber.Value(1,100)
msgbox n

7. ## Re: a simple question about randomnumber

[ QUOTE ]
Why not use QTP's inbuilt random number generator:

x=RandomNumber (0,100)

This will generate a random number between 0 &amp; 100

Mark Smith.

[/ QUOTE ]

yes, you are right, thanks a lot!

8. ## Re: a simple question about randomnumber

There is syntax Errors in rand(k,n)) Function of initial post:
1. one word missed [Randomize]
2. Function should return value

Try this [corrected]:

function rand(k,n)
Randomize
rand = int((k-n+1)*rnd+n)
end function

Janna

9. ## Re: a simple question about randomnumber

you are right!!!
thank you very very much, That is just what I want.

10. ## Re: a simple question about randomnumber

Dim MyValue, Response
Randomize ' Initialize random-number generator.
Do Until Response = vbNo
MyValue = Int((10000 * Rnd) + 1) ' Generate random value between 1 and 10000.
MsgBox MyValue
Response = MsgBox ("Roll again? ", vbYesNo)
Loop

Look up the help file lots of information.

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.