Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
5 years ago

[TechCorner Challenge #9] Generate a random number within a range

Hi Community!

 

Another awesome task is ready๐Ÿ™‚

 

By completing the task, you will not only learn TestComplete features but also get into the TechCorner Leaderboard.

 

Participate in the challenge and earn new unique badges! Check out some of these awesome rewards:

 

Let's imagine that you are testing an application that has gaming elements - a knight is using a legendary sword to slay a dragon! And, the sword will deliver damage in the following range - from 30 to 75. 

 

Instead of using the same number every time, you want to generate a random number on each run and enter it in the application. How would you do it in TestComplete?


Let's see if we can get an example for each scripting language (JavaScript, JScript, Python, VBScript)! 

Task: Generate a random number within the following range (30-75) in TestComplete using a scripting language of your choice. 

Difficulty:

 

Bonus: You will get an additional point for each script written in a different language.

 

Best of luck!

  • Task: Generate a random number within the following range (30-75) in TestComplete using a scripting language of your choice. 

     

    This is a solution created for [TechCorner Challenge #9]

     

    I used math random() method in javascript to do that, let me try to do it in another language as well.

     

     

     

    //JavaScript
    //JScript
    
    
    
    function test()
    {
     Log.Message(getRandomNumber(30,75))
    }
    
    
    
    function getRandomNumber(min, max)
    
    {
      return Math.floor(Math.random() * (max - min + 1) ) + min;
    }

     

     

  • Task: Generate a random number within the following range (30-75) in TestComplete using a scripting language of your choice. 

     

    This is a solution created for [TechCorner Challenge #9]

     

    The following Vb Script code demonstrates how to generate random integers that fall within a specified range from 30 to 75. Here, I have used 'Rnd' function 

     

     

     

     

    'VBScript
    
    Function RandomNumber_In_givenRange()
    Dim Minimum
    Dim Maximum
    Minimum = 30
    Maximum = 75
    Randomize
    Log.Message Int((Maximum - Minimum + 1) * Rnd + Minimum)
    End Function

     

     

     

     

  • Task: Generate a random number within the following range (30-75) in TestComplete using a scripting language of your choice. 

     

    This is a solution created for [TechCorner Challenge #9]

     

    Hi folks, I gather you just want to have the value returned, not actually automate input of that value into an application?

     

    If so, here's some Python.

     

     

     

     

     

    #python
    
    from random import randrange
    
    Log.Message(randrange(30, 75, 1))

     

     

     

     

     

  • mkambham's avatar
    mkambham
    Occasional Contributor

    Task: Generate a random number within the following range (30-75) in TestComplete using a scripting language of your choice. 

     

    This is a solution created for [TechCorner Challenge #9]

     

    The following Vb Script code demonstrates how to generate random integers that fall within a specified range from 30 to 75. Here, I have used 'Rnd' function 

     

     

     

     

    'VBScript
    
    Function RandomNumber_In_givenRange()
    Dim Minimum
    Dim Maximum
    Minimum = 30
    Maximum = 75
    Randomize
    Log.Message Int((Maximum - Minimum + 1) * Rnd + Minimum)
    End Function

     

     

     

     

  • anupamchampati's avatar
    anupamchampati
    Frequent Contributor

    Task: Generate a random number within the following range (30-75) in TestComplete using a scripting language of your choice. 

     

    This is a solution created for [TechCorner Challenge #9]

     

    I used math random() method in javascript to do that, let me try to do it in another language as well.

     

     

     

    //JavaScript
    //JScript
    
    
    
    function test()
    {
     Log.Message(getRandomNumber(30,75))
    }
    
    
    
    function getRandomNumber(min, max)
    
    {
      return Math.floor(Math.random() * (max - min + 1) ) + min;
    }

     

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      anupamchampati Awesome! Thank you for participation!

      JavaScript is done๐Ÿ™‚ Which other language would you like to try doing this task for?

       

       

  • Wrongtown's avatar
    Wrongtown
    Occasional Visitor

    Task: Generate a random number within the following range (30-75) in TestComplete using a scripting language of your choice. 

     

    This is a solution created for [TechCorner Challenge #9]

     

    Hi folks, I gather you just want to have the value returned, not actually automate input of that value into an application?

     

    If so, here's some Python.

     

     

     

     

     

    #python
    
    from random import randrange
    
    Log.Message(randrange(30, 75, 1))

     

     

     

     

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      mkambham Wrongtown great job! Your scripts work like magic.

       

      Looks like only JScript is left! Will you give it a try?๐Ÿ™‚

      • anupamchampati's avatar
        anupamchampati
        Frequent Contributor
        Sorry for been late
        Script which is written for javascript should for jscript as well.
  • mkambham's avatar
    mkambham
    Occasional Contributor

    Same Script for JScript and JavaScript

     

    the Code is below.

     

     


    function RandomNumber_between_Range()
    {
    var min = 30;
    var max = 75;

    RandomNumber = Math.floor(Math.random()*(max-min+1)+min);

    Log.Message(RandomNumber)
    }