1

Below is my code, I want to get random number between 1 and 10, but it returns like 2.0 not 2

    Scenario: POST /api/coupon/status/change__Activate
     Given path '/api/coupon/be/save'
     And headers allHeaders.HeadersToken
     * def GetNum = function(){num=Math.round(Math.random() * 9) + 1); return num;}
     * def BrandCode = GetNum()
     * print BrandCode
And I try to convert result, it failed. 
    * def GetNum = function(){int num=(int)(Math.round(Math.random() * 9) + 1)); return num;}
phuclv
  • 32,499
  • 12
  • 130
  • 417
lllzhuo
  • 21
  • 3
  • I think you are new at Stack Overflow. can you kindly mark this answer as accepted (and also upvote) if it helped you: https://stackoverflow.com/a/48338127/143475 – Peter Thomas Jan 23 '18 at 08:46

1 Answers1

2

@lllzhuo - I found if I just concatenated an empty string to the random() result, the decimal was dropped:

* def random = function(max){ return Math.floor(Math.random() * max) + 1 }
* def index = random(5) + ''
* print index

I also found if I use the random() call directly in an array it successfully (ignored) the decimal:

* def print result[random(5)]
kpschmidt
  • 245
  • 2
  • 9