Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Randomize a one dimentional array

Reply
 
Old 10-02-2008   #1 (permalink)
Bob Smith


 
 

Randomize a one dimentional array

Does anyone know how I can randomize a one dimentional array with VBscript. I
would like it to pull back the values with no duplicates.

e.g.

mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test"
myarr = split(mystring,vbcrlf)


My System SpecsSystem Spec
Old 10-02-2008   #2 (permalink)
Auric__


 
 

Re: Randomize a one dimentional array

On Thu, 02 Oct 2008 16:30:01 GMT, =?Utf-8?B?Qm9iIFNtaXRo?= wrote:
Quote:

> Does anyone know how I can randomize a one dimentional array with
> VBscript. I would like it to pull back the values with no duplicates.
>
> e.g.
>
> mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test"
> myarr = split(mystring,vbcrlf)
Copy the items in random order from the source array to a second array. As
each item is copied from the source array, replace it with the last item in
the array, then decrease the size of the array by 1. When you hit 0, you're
done.

There are better ways to do this, of course, but this is a very simple
method that I've used in a few places. This code should do what you want:

RANDOMIZE TIMER
source = ARRAY("this", "is", "a", "test")
ubSrc = UBOUND(source)

REDIM target(ubSrc)

FOR L0 = ubSrc TO 1 STEP -1 ' the actual work is in this loop
tmp = INT(RND * (L1 + 1))
target(L1) = source(tmp)
source(tmp) = source(L1)
REDIM PRESERVE source(L1 - 1)
NEXT

target(0) = source(0)

outP = target(0)
FOR L0 = 1 TO ubSrc
outP = outP & VBCRLF & target(L1)
NEXT

MSGBOX outP

This works for many similar situations, i.e. card shuffling.

--
What is the difference between Nintendo64 and Commodore64?
My System SpecsSystem Spec
Old 10-02-2008   #3 (permalink)
ekkehard.horner


 
 

Re: Randomize a one dimentional array

Bob Smith schrieb:
Quote:

> Does anyone know how I can randomize a one dimentional array with VBscript. I
> would like it to pull back the values with no duplicates.
>
> e.g.
>
> mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test"
> myarr = split(mystring,vbcrlf)
>
See topic "How to Randomize characters in a string" (2008-08-19)
in this group
My System SpecsSystem Spec
Old 10-02-2008   #4 (permalink)
ekkehard.horner


 
 

Re: Randomize a one dimentional array

Auric__ schrieb:
Quote:

> On Thu, 02 Oct 2008 16:30:01 GMT, =?Utf-8?B?Qm9iIFNtaXRo?= wrote:
>
Quote:

>> Does anyone know how I can randomize a one dimentional array with
>> VBscript. I would like it to pull back the values with no duplicates.
>>
>> e.g.
>>
>> mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test"
>> myarr = split(mystring,vbcrlf)
>
> Copy the items in random order from the source array to a second array. As
> each item is copied from the source array, replace it with the last item in
> the array, then decrease the size of the array by 1. When you hit 0, you're
> done.
>
> There are better ways to do this, of course, but this is a very simple
> method that I've used in a few places. This code should do what you want:
>
> RANDOMIZE TIMER
> source = ARRAY("this", "is", "a", "test")
> ubSrc = UBOUND(source)
>
> REDIM target(ubSrc)
>
> FOR L0 = ubSrc TO 1 STEP -1 ' the actual work is in this loop
> tmp = INT(RND * (L1 + 1))
> target(L1) = source(tmp)
> source(tmp) = source(L1)
> REDIM PRESERVE source(L1 - 1)
Because L1 is not initialized/empty/0, source is clobbered
here immediately: (L1 - 1) == -1.
Quote:

> NEXT
>
> target(0) = source(0)
>
> outP = target(0)
> FOR L0 = 1 TO ubSrc
> outP = outP & VBCRLF & target(L1)
> NEXT
>
> MSGBOX outP
>
> This works for many similar situations, i.e. card shuffling.
>
My System SpecsSystem Spec
Old 10-02-2008   #5 (permalink)
Bob Smith


 
 

Re: Randomize a one dimentional array

Thanks Auric__ but the code does not seem to work. subscript out of range:
'tmp'. I'm not sure why but it looks like value L1 is not defined? What
should L1 be?



"Auric__" wrote:
Quote:

> On Thu, 02 Oct 2008 16:30:01 GMT, =?Utf-8?B?Qm9iIFNtaXRo?= wrote:
>
Quote:

> > Does anyone know how I can randomize a one dimentional array with
> > VBscript. I would like it to pull back the values with no duplicates.
> >
> > e.g.
> >
> > mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test"
> > myarr = split(mystring,vbcrlf)
>
> Copy the items in random order from the source array to a second array. As
> each item is copied from the source array, replace it with the last item in
> the array, then decrease the size of the array by 1. When you hit 0, you're
> done.
>
> There are better ways to do this, of course, but this is a very simple
> method that I've used in a few places. This code should do what you want:
>
> RANDOMIZE TIMER
> source = ARRAY("this", "is", "a", "test")
> ubSrc = UBOUND(source)
>
> REDIM target(ubSrc)
>
> FOR L0 = ubSrc TO 1 STEP -1 ' the actual work is in this loop
> tmp = INT(RND * (L1 + 1))
> target(L1) = source(tmp)
> source(tmp) = source(L1)
> REDIM PRESERVE source(L1 - 1)
> NEXT
>
> target(0) = source(0)
>
> outP = target(0)
> FOR L0 = 1 TO ubSrc
> outP = outP & VBCRLF & target(L1)
> NEXT
>
> MSGBOX outP
>
> This works for many similar situations, i.e. card shuffling.
>
> --
> What is the difference between Nintendo64 and Commodore64?
>
My System SpecsSystem Spec
Old 10-02-2008   #6 (permalink)
Auric__


 
 

Re: Randomize a one dimentional array

On Thu, 02 Oct 2008 17:57:20 GMT, ekkehard.horner wrote:
Quote:

> Auric__ schrieb:
Quote:

>> On Thu, 02 Oct 2008 16:30:01 GMT, =?Utf-8?B?Qm9iIFNtaXRo?= wrote:
>>
Quote:

>>> Does anyone know how I can randomize a one dimentional array with
>>> VBscript. I would like it to pull back the values with no duplicates.
>>>
>>> e.g.
>>>
>>> mysting = "this" & vbcrlf & "is" & vbcrlf & "a" & vbcrlf & "test"
>>> myarr = split(mystring,vbcrlf)
>>
>> Copy the items in random order from the source array to a second array.
>> As each item is copied from the source array, replace it with the last
>> item in the array, then decrease the size of the array by 1. When you
>> hit 0, you're done.
>>
>> There are better ways to do this, of course, but this is a very simple
>> method that I've used in a few places. This code should do what you
>> want:
>>
>> RANDOMIZE TIMER
>> source = ARRAY("this", "is", "a", "test")
>> ubSrc = UBOUND(source)
>>
>> REDIM target(ubSrc)
>>
>> FOR L0 = ubSrc TO 1 STEP -1 ' the actual work is in this loop
>> tmp = INT(RND * (L1 + 1))
>> target(L1) = source(tmp)
>> source(tmp) = source(L1)
>> REDIM PRESERVE source(L1 - 1)
>
> Because L1 is not initialized/empty/0, source is clobbered
> here immediately: (L1 - 1) == -1.
Whoops. That's a think-o. Change all instances of L1 to L0 (or L0 to L1,
doesn't matter). Should be:

FOR L0 = ubSrc TO 1 STEP -1
tmp = INT(RND * (L0 + 1))
target(L0) = source(tmp)
source(tmp) = source(L0)
REDIM PRESERVE source(L0 - 1)
NEXT

--
There might be room at the top but there is seldom enough time.
My System SpecsSystem Spec
Old 10-02-2008   #7 (permalink)
Auric__


 
 

Re: Randomize a one dimentional array

On Thu, 02 Oct 2008 18:14:12 GMT, =?Utf-8?B?Qm9iIFNtaXRo?= wrote:
Quote:

> Thanks Auric__ but the code does not seem to work. subscript out of
> range: 'tmp'. I'm not sure why but it looks like value L1 is not
> defined? What should L1 be?
As ekkehard.horner pointed out, there's a screw-up in what I posted.
Corrected version, works for me:

RANDOMIZE TIMER
source = ARRAY("this", "is", "a", "test")
ubSrc = UBOUND(source)

REDIM target(ubSrc)

FOR L1 = ubSrc TO 1 STEP -1
tmp = INT(RND * (L1 + 1))
target(L1) = source(tmp)
source(tmp) = source(L1)
REDIM PRESERVE source(L1 - 1)
NEXT

target(0) = source(0)

outP = target(0)
FOR L1 = 1 TO ubSrc
outP = outP & VBCRLF & target(L1)
NEXT

MSGBOX outP

(That's what I get for posting before my daily caffiene IV. [sigh])

--
Welcome and keep out.
My System SpecsSystem Spec
Old 10-02-2008   #8 (permalink)
ekkehard.horner


 
 

Re: Randomize a one dimentional array

Auric__ schrieb:
Quote:

> On Thu, 02 Oct 2008 17:57:20 GMT, ekkehard.horner wrote:
>
Quote:

>> Auric__ schrieb:
[...]
Please test your code before posting - now you generate an
array with 4 identical string items.
My System SpecsSystem Spec
Old 10-02-2008   #9 (permalink)
ekkehard.horner


 
 

Re: Randomize a one dimentional array

ekkehard.horner schrieb:
Quote:

> Auric__ schrieb:
Quote:

>> On Thu, 02 Oct 2008 17:57:20 GMT, ekkehard.horner wrote:
>>
Quote:

>>> Auric__ schrieb:
> [...]
> Please test your code before posting - now you generate an
> array with 4 identical string items.
Sorry, my fault: you generate a correct array, but the display
code repeats the L1==0==uninitialized value
My System SpecsSystem Spec
Old 10-02-2008   #10 (permalink)
Auric__


 
 

Re: Randomize a one dimentional array

On Thu, 02 Oct 2008 19:00:53 GMT, ekkehard.horner wrote:
Quote:

> ekkehard.horner schrieb:
Quote:

>> Auric__ schrieb:
Quote:

>>> On Thu, 02 Oct 2008 17:57:20 GMT, ekkehard.horner wrote:
>>>
>>>> Auric__ schrieb:
>> [...]
>> Please test your code before posting - now you generate an
>> array with 4 identical string items.
> Sorry, my fault: you generate a correct array, but the display
> code repeats the L1==0==uninitialized value
Please see my second response to the OP. (Me - caffiene = bad.)

I should've just posted my actual code (a card shuffling test) that does
this:

-----begin shufler4.vbs-----
Option Explicit

Const ForReading = 1
Const ForWriting = 2

Dim unShuffled
Dim Shuffled(51)
Dim L0, card, fO
Dim FSO

unShuffled = Array("AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", _
"10S", "JS", "QS", "KS", "AH", "2H", "3H", "4H", "5H", "6H", "7H", _
"8H", "9H", "10H", "JH", "QH", "KH", "AC", "2C", "3C", "4C", "5C", _
"6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", "AD", "2D", "3D", _
"4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD")

Randomize Timer

For L0 = 51 To 0 Step -1
card = Int(Rnd * (L0 + 1))
Shuffled(L0) = unShuffled(card)
If card <> L0 Then unShuffled(card) = unShuffled(L0)
Next

Set FSO = WScript.CreateObject("Scripting.FileSystemObject")

Set fO = FSO.OpenTextFile("shuf4.txt", ForWriting, True)
For L0 = 0 To 51
fO.WriteLine Shuffled(L0)
Next
fO.Close
-----end shufler4.vbs-----

(Note that I don't resize the array in this version.)

--
kill -9 needs no justification!
-- BOfH
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Fast copy method of sub array (=array range) possible? VB Script
Search a Two-Dimentional Array VB Script
Stupid Array Tricks: Initializing an Array to a Certain Size PowerShell
How to Randomize characters in a string VB Script
how to assign values to array and how to create array via variable PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46