Thanks:  0
Likes:  0
Dislikes:  0

1. ## Randomize

User =?iso-8859-1?q?ss=20rr?= (syedrational2002@yahoo.co.in.nospam) posted:

Hi All,

I have got an array from which i want to retrieve the
values randomly.How to use the Randomize function for
this?

I tried using this but it didnt worked out.Do we have
to use Rnd Function along with in?

Kindly send me the code if any

__________________________________________________ ______________________
at: http://in.sports.yahoo.com/cricket/tracker.html

---

2. ## Re: Randomize

User Gustavo Rojas (grojas@microsoft.com.nospam) posted:

You can use a combination of the Rnd and Randomize function.
When using the Rnd function, it will always return the same set of
numbers until you run the Randomize function to initialize the random
number generator. You can create your random indexes like this:

' For zero-based arrays
iIndex = Int(Rnd() * HighestToReturn+1)

where HighestToReturn+1 is the highest index you would like to access +
1.
If you want to access indexes from 0 to 13, replace HighestToReturn+1
with 14. If you would like to retrieve any of the items in your array,
replace HighestToReturn+1 with UBound(_Your_Array_) + 1. The '+ 1' is to
include the highest element selected.

' For non-zero based arrays and/or specific range search
iIndex = Int(Rnd() * Par1) + Par2
*** For 1-based arrays: Par1 is the highest index you would like to
access and Par2 would be 1. If you would like to retrieve any of the
items in your array, replace Par1 with UBound(_Your_Array_) and keep
Par2 at 1.

*** For specific range search: make Par2 the lowest index you would like
to retrieve and Par1 equal to the highest index to retrieve minus Par2
plus 1. Say you want to return items from 3 to 17, then you would make
Par2 = 3 and Par1 would equal 17 - 3 + 1, which is 15. The '+ 1' is to
include index 17. If you want to access from 5 to the end of your array,
Par2 would be 5, Par1 would be UBound(_Your_Array_) - Par2 + 1.

Gustavo Rojas
____________________________________
Software Test Engineer
Solomon Platform Services Group

3. ## Re: Randomize

User Tom Roberts (testmachine2k@yahoo.com.nospam) posted:

WOW! This is is good stuff.

--- Gustavo Rojas <grojas@microsoft.com> wrote:
__________________________________________________ ______________________
************************************************** **********************
************************************************** **********************
************************************************** **************************
************************************************** ***************************

__________________________________________________

4. ## Re: Randomize

User (MMcNaughton@STSSystems.com.nospam) posted:

Something like this,

iMax = UBound(arArray)
iMin = LBound(arArray)
Randomize Timer
iTemp = Int((iMax - iMin + 1) * Rnd) + iMin
RandomArrayValue = arArray(iTemp)

HTH,
Max

5. ## Re: Randomize

User (MMcNaughton@STSSystems.com.nospam) posted:

Only sent the response below 2 days ago, see how long this one takes!

However, since i'm on my home comp, here's the code i had at home that i was

trying to remember.

The simple one time random retrieval would be, everytime you want a random
value is:

Randomize Timer
iRandomIndex = Int(Ubound(arArray) * Rnd)

Then you use the iRandomIndex with your array
arArray(iRandomIndex)

...
The code below is specifically for shuffling indexes for later use with
recordsets, but you'd simply have to in the first portion assign the values
in the array to the temp array and that should do it.

Just takes the contents of the array and shuffles them around, no
repetition. If you step through with the variable window on, you'll see how

the array uses random substitution to do the job.

'Populate array and Temp Copy
For iCnt = 0 To UBound(arArray)
arArray(iCnt) = iCnt 'for your purpose
arTempArray(iCnt) = iCnt 'arTempArray(iCnt) = arArray(iCnt)
Next
iMax = UBound(arArray)
iMin = LBound(arArray)

'Fill arArray() with a shuffled set of row numbers
For iCnt = iMax to iMin Step -1
Randomize Timer
iTemp = Int((iMax - iMin + 1) * Rnd) + iMin
arTempArray(iCnt) = arArray(iTemp)
arArray(iTemp) = arArray(iCnt)
arArray(iCnt) = arTempArray(iCnt)
DoEvents
Next iCnt

HTH,
Max
-----Original Message-----
From: MMcNaughton@STSSystems.com
To: SQA Suite Team Test Users
Sent: 4/10/2002 9:19 AM
Subject: RE: Randomize

Something like this,

iMax = UBound(arArray)
iMin = LBound(arArray)
Randomize Timer
iTemp = Int((iMax - iMin + 1) * Rnd) + iMin
RandomArrayValue = arArray(iTemp)

HTH,
Max

-----Original Message-----
From: ss rr [mailto:syedrational2002@yahoo.co.in]
Sent: Wednesday, April 10, 2002 6:53 AM
To: SQA Suite Team Test Users
Subject: Randomize

Hi All,

I have got an array from which i want to retrieve the
values randomly.How to use the Randomize function for
this?

I tried using this but it didnt worked out.Do we have
to use Rnd Function along with in?

Kindly send me the code if any

---

6. ## Re: Randomize

User Tom Ba (tombasay@hotmail.com.nospam) posted:

Tomba observe this phenomenon at 4:45 AM
(before coffee)
and could only think
daylight saving time;
you guys set your clocks back
a WHOLE day, eh?

Bravo!

It about time we do the same.

No more clock resetting.

No more disorientation,
stumbling,
falling down,
getting up,
kicking dog,
losing toes.

you gain a whole freakinâ€™ DAY.

It obviously working.
It Friday,
Tomba
Can we go back

----Original Message Follows----
From: MMcNaughton@STSSystems.com
Reply-To: "SQA Suite Team Test Users" <sqa-suite-users@lyris.dundee.net>
To: "SQA Suite Team Test Users" <sqa-suite-users@lyris.dundee.net>
Subject: RE: Randomize
Date: Fri, 12 Apr 2002 11:10:40 -0400

Only sent the response below 2 days ago, see how long this one takes!

However, since i'm on my home comp, here's the code i had at home that i was

trying to remember.
__________________________________________________ _______________
MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx

---

7. ## Re: Randomize

User (MMcNaughton@STSSystems.com.nospam) posted:

;o) Excellent aye,

Max
-----Original Message-----
From: Tom Ba
To: SQA Suite Team Test Users
Sent: 4/12/2002 12:20 PM
Subject: otRE: Randomize

Tomba observe this phenomenon at 4:45 AM
(before coffee)
and could only think
daylight saving time;
you guys set your clocks back
a WHOLE day, eh?

Bravo!

It about time we do the same.

No more clock resetting.

No more disorientation,
stumbling,
falling down,
getting up,
kicking dog,
losing toes.

you gain a whole freakin=E2EUR(tm) DAY.

It obviously working.
It Friday,

Tomba
Can we go back

---

8. ## Re: Randomize

User John Fodeh (jaf@bkmed.dk.nospam) posted:

If you want to avoid getting the same value twice, you might find the
function underneath useful. It will shuffle the contents of a string
array. If you want to reproduce the same order, provide the function with
the same seed.

Option Explicit

Function ShuffleArray(strArrayToShuffle() As String, Optional Seed) As
Integer
'************************************************* *******************
'Purpose: To shuffle the contents of an array randomly.
' If a seed value is provided the function uses this value
' for the Rnd function's random-number generator.
'Inputs :
' strArrayToShuffle - the array to be shuffled
' Seed - Optional, the Randomize function uses the seed number
' to initialize the Rnd function's random-number generator.
' If omitted, the value returned by the system timer is used
'Assumes: NA.
'Returns: -1 if successful, 0 if failure
'Affects: NA.
'************************************************* *******************
'Developed by: John A. Fodeh B-K Medical A/S © 2001
'Revision History:
'Date Initials Amendment
'DD-MM-YY What & Why
'--------------------------------------------------------------------
'22-02-01 JAF Created
'************************************************* *******************
Dim blnArray() As Integer 'Control array
Dim tmpArray() 'Duplicate of original array
Dim lngUpperLimit, lngLowerLimit As Long
Dim lngArrayIndex As Long
Dim lngSeedVal As Long
Dim lngRandomIndex As Long
Dim strErrMsg As String

On Error GoTo ErrHandler 'Trap unexpected errors

lngUpperLimit = UBound(strArrayToShuffle)
lngLowerLimit = LBound(strArrayToShuffle)

'Initialize arrays
ReDim blnArray(lngLowerLimit To lngUpperLimit)
ReDim tmpArray(lngLowerLimit To lngUpperLimit)
For lngArrayIndex = lngLowerLimit To lngUpperLimit
blnArray(lngArrayIndex) = 0
tmpArray(lngArrayIndex) = strArrayToShuffle(lngArrayIndex)
Next lngArrayIndex

'Initialize the random-number generator.
If IsMissing(Seed) Then
lngSeedVal = Timer
Else
lngSeedVal=Val(Seed)
End If

SQAConsoleWrite "ShuffleArray:: Random seed " & lngSeedVal

Randomize lngSeedVal

For lngArrayIndex = lngLowerLimit To lngUpperLimit
Do
'Get random array index
lngRandomIndex = Int((lngUpperLimit - lngLowerLimit + 1) * Rnd) _
+ lngLowerLimit

'SQAConsoleWrite "Found random index: " & lngRandomIndex

If blnArray(lngRandomIndex) = 0 Then
strArrayToShuffle(lngRandomIndex) =
tmpArray(lngArrayIndex)
blnArray(lngRandomIndex) = -1
Exit Do
End If
Loop
Next

SQAConsoleWrite "Array shuffle completed!"
ShuffleArray = -1

Done:
Exit Function

ErrHandler:
ShuffleArray = 0
strErrMsg = "ShuffleArray:: " & Error\$ & " occurred at line: " & Erl
SQALogMessage SqaFail, strErrMsg, "Error code: " & Err
End Function

Sub main
Dim astrArray(0 to 8) As String
Dim intIndex As Integer

SQAConsoleWrite "Before Shuffle:"
For intIndex=0 to 8
astrArray(intIndex) = Chr(Asc("a") + intIndex)
SQAConsoleWrite intIndex & ": " & astrArray(intIndex)
Next intIndex

If ShuffleArray(astrArray) = 0 then
Msgbox "Failed to shuffle array!"
Exit Sub
End if

SQAConsoleWrite Chr(13) & "After Shuffle:"
For intIndex=0 to 8
SQAConsoleWrite intIndex & ": " & astrArray(intIndex)
Next intIndex

End Sub

Best regards
John Fodeh,
B-K Medical

---

#### 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.