Thanks:  0
Likes:  0
Dislikes:  0

Thread: how to get a values in a random way

1. how to get a values in a random way

I am new to the Qa field, I have a testcase where we have about 10/15 values which needs to be pulled out in a random way each time a test is run, right now I have put all the data in a data table & pulling out the data in sequentially.

------------------

2. Re: how to get a values in a random way

Here's the way I do it.

At the beginning of the test, read all of your data into an array.

When you need a random value, generate a random offset into the array ( see rand() in the WinRunner help file ) and use the values associated contained in the array at the randomly generate offset.

It may not be a perfect solution, but it works well.

3. Re: how to get a values in a random way

This code is ugly and un-tested...

You should have No duplications using this function. Each number will only be used once...it's magic

# This function will populate an array with values in the range "iMin" thru "iMax".
# if iMin=1 and iMax=15 you should get an Array of ArrayX[0] thru ArrayX[14]
public function getRandom(in iMin, in iMax, out aRand[])
{
auto iRange, loopX, i, x, Found, rc1, randomX, randomY;

if (iMin < 0 &#0124; &#0124; iMin > iMax &#0124; &#0124; (iMin + iMax)==0)
return -1;

if (int(iMin) != iMin &#0124; &#0124; int(iMax) != iMax)
return -1;

srand(GetTickCount());
iRange = (iMax - iMin) + 1;
i = 0;
loopX = iMax - 1;
while ( i < loopX )
{
randomX = int(rand() * iRange) + iMin;
if ( length(aRand[0]) < 1 )
{
aRand[0] = randomX;
}

x = 0;
Found = 0;
for (x in aRand)
{
randomY = aRand[x];
if (randomY == randomX)
Found = 1;
}
if (Found != 1)
{
i++;
aRand[i] = randomX;
}
}
return 0;
}

reload ( "C:\\WinRunner\\lib\\win32api", 1, 1 );

rc1 = getRandom(1, 15, ArrayX);

for (xx in ArrayX)
countX++;

# ArrayX should contain numbers 1 to 15 with No duplications.

# if you get the data from the table using "ddt_val_by_row" you can use the numbers in ArrayX. "countX" contains the number of elements in ArrayX.
ArrayX[0] thru ArrayX[14]

------------------

4. Re: how to get a values in a random way

If you want a quick and dirty method that is only sort of random, you could put the data into an array and then use the 'in' operator to extract it:

for(ix in myArray) {
# do something with myArray[ix];
}

When 'in' is used this way, it cycles through the array in a system-determined manner which will not be sequential from myArray[0] to myArray[n], though it's probably not truly random either.

------------------
Charles Reace

charles{DOT}reace{AT}verizon{DOT}net

5. Re: how to get a values in a random way

Thanks for the suggestions.

------------------

6. Re: how to get a values in a random way

Here is the code that I am trying to execute for this getting a random value for each iteration, But all the time I am getting the same output value;

public my_array []= {"a","b","c","d"};
for (i in my_array)
{
pause (my_array[i]);
}

print (my_array[i]);

Could someone tell me what is wrong in this or Is there a better way to get a random value each time a test is run.

------------------

7. Re: how to get a values in a random way

<BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by novice:
Here is the code that I am trying to execute for this getting a random value for each iteration, But all the time I am getting the same output value;

public my_array []= {"a","b","c","d"};
for (i in my_array)
{
pause (my_array[i]);
}

print (my_array[i]);

Could someone tell me what is wrong in this or Is there a better way to get a random value each time a test is run.

<HR></BLOCKQUOTE>

try something like:

pause (my_array[int(rand()*4)]);

(assuming you have 4 elements in your array)

Also, read up on the srand() function if you want the selection to be different even when you shut down WinRunner and bring it back up again.

------------------
- Joe (strazzerj@aol.com)

8. Re: how to get a values in a random way

OK, this might not be the most elegant code in the world, but it seems to work. It picks out each element of my_array[] once, and only once, in a random order. Hope this helps....

<pre>public my_array[] = {"a","b","c","d"};
public indices[];
size = 0; # will hold size of indices[]
for(ix in my_array) {
indices[ix] = ix;
size++;
}
srand();
while(size > 0) {
this1 = int(rand() * size);
counter = 0;
for(ix in indices) {
if(counter == this1) {
printf("%s", my_array[indices[ix]]);
delete indices[ix];
}
counter++;
}
size--;
}</pre>

PS: I've converted the above logic into a function, see the attachment. Enjoy!
------------------
Charles Reace

charles{DOT}reace{AT}verizon{DOT}net

[This message has been edited by Charles Reace (edited 04-24-2002).]

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.