[TechCorner Challenge #9] Generate a random number within a range
SOLVED- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[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
Solved! Go to Solution.
- Labels:
-
Scripting
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
}
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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))
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Script which is written for javascript should for jscript as well.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
@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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
}
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Great job.
