Ask a Question

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

SOLVED
sonya_m
SmartBear Alumni (Retired)

[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:

TestComplete-TechCorner-Takeoff.pngTestComplete-TechCorner-Contributor.png

 

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:Star_Gold.pngStar_gray.pngStar_gray.png

 

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

 

Best of luck!


Sonya Mihaljova
Community and Education Specialist

10 REPLIES 10
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
SmartBear Alumni (Retired)

@anupamchampati Awesome! Thank you for participation!

JavaScript is done🙂 Which other language would you like to try doing this task for?

 

 


Sonya Mihaljova
Community and Education Specialist

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

 

 

 

 

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
SmartBear Alumni (Retired)

@mkambham @Wrongtown great job! Your scripts work like magic.

 

Looks like only JScript is left! Will you give it a try?🙂


Sonya Mihaljova
Community and Education Specialist

anupamchampati
Frequent Contributor

Sorry for been late
Script which is written for javascript should for jscript as well.
sonya_m
SmartBear Alumni (Retired)

@anupamchampati Thank you for checking that this works for JScript, too! A Bonus Point for this task is yours🙂


Sonya Mihaljova
Community and Education Specialist

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)
}

 

gavemula
Occasional Contributor

Great job.

cancel
Showing results for 
Search instead for 
Did you mean: