cancel
Showing results for
Search instead for
Did you mean:

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

SOLVED
Community Manager

## [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!

Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Tags (1)
3 ACCEPTED SOLUTIONS

Accepted Solutions
Frequent Contributor

## Generate a random number within a range

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;
}``````

Tags (1)
Occasional Contributor

## Generate a random number within a range

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``````

Tags (1)
Occasional Visitor

## Generate a random number within a range

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

Tags (1)
10 REPLIES 10
Frequent Contributor

## Generate a random number within a range

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;
}``````

Tags (1)
Community Manager

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

@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

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Occasional Contributor

## Generate a random number within a range

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``````

Tags (1)
Occasional Visitor

## Generate a random number within a range

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

Tags (1)
Community Manager

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

@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

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Frequent Contributor

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

Sorry for been late
Script which is written for javascript should for jscript as well.
Community Manager

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

@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

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Occasional Contributor

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

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

Occasional Contributor

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

Great job.

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors
Interests
Latest Topics