SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Results 1 to 10 of 10
  1. #1
    Member
    Join Date
    May 2007
    Posts
    152
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Random number generators

    To generate a random number, I would use:

    mynum = (6 * Rnd) + 1

    From the code, i can see that the number generated will be a float with several numbers after decimal. Is there a way to generate a random number with 3 decimals, for example?

    Thanks.

  2. #2
    Advanced Member
    Join Date
    Mar 2007
    Location
    Salt Lake City, UT, USA
    Posts
    606
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Random number generators

    mynum = int(6000 * Rnd + 1) / 1000
    "The last 10% of any software project will take 90% of the budgeted time. The first 90% will take the other 90%"

  3. #3
    Member
    Join Date
    May 2007
    Posts
    152
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Random number generators

    I figures that one. Also i thought of generating 2 numbers then putting them together. but is there a decimal limiter on the RNd function?

  4. #4
    Senior Member
    Join Date
    Mar 2006
    Location
    Bristol, UK
    Posts
    1,530
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Random number generators

    I use this (stolen from QTP help!);

    number = Int((upperbound - lowerbound +1) * RND + Lowerbound)

    It always returns a whole number

  5. #5
    Advanced Member
    Join Date
    Mar 2007
    Location
    Salt Lake City, UT, USA
    Posts
    606
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Random number generators

    [ QUOTE ]
    I figures that one. Also i thought of generating 2 numbers then putting them together. but is there a decimal limiter on the RNd function?

    [/ QUOTE ]

    I doubt it.

    Rnd() is a fairly pure concept. Generate a pseudo-random number between 0 and 1. Once you get that number, it's up to you to decide what you want to do with it - multipliers, limits, truncations, whatever you want.

    I suppose you could always create your own function. A wrapper for the Rnd() function. Register it and call it as you like.

    Or just handle it in your script.
    "The last 10% of any software project will take 90% of the budgeted time. The first 90% will take the other 90%"

  6. #6
    Senior Member
    Join Date
    Sep 2001
    Location
    Chicago
    Posts
    161
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Random number generators

    Try this:
    number = Round(((5- 1 +1) * RND + 1) , 3)

    where number = 4.528
    ififthelement
    --------------
    "life is simple if you can see both sides of a coin" - by me

  7. #7
    Member
    Join Date
    May 2007
    Posts
    152
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Random number generators

    [ QUOTE ]
    Try this:
    number = Round(((5- 1 +1) * RND + 1) , 3)

    where number = 4.528

    [/ QUOTE ]Thats it!

  8. #8
    Moderator
    Join Date
    Sep 2001
    Location
    Doncaster, UK
    Posts
    5,788
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Random number generators

    Or use QTP's inbuilt random number generator, this is from the help:

    The following example generates a random number between 0 and 100.

    x=RandomNumber (0,100)


    Mark Smith.

  9. #9
    Member
    Join Date
    May 2007
    Posts
    152
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Random number generators

    [ QUOTE ]
    Or use QTP's inbuilt random number generator, this is from the help:

    The following example generates a random number between 0 and 100.

    x=RandomNumber (0,100)


    Mark Smith.

    [/ QUOTE ]wouldnt that return a whole number?

  10. #10
    Moderator
    Join Date
    Sep 2001
    Location
    Doncaster, UK
    Posts
    5,788
    Post Thanks / Like
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: Random number generators

    Sorry mSky, yes it would.

    I must learn to read all of the post, not just the 1st 2 lines.

    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
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 10.00%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 05:38 AM.

Copyright BetaSoft Inc.