Windows Vista Forums

Randomize a one dimentional array
  1. #1


    Bob Smith Guest

    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

  2. #2


    Auric__ Guest

    Re: Randomize a one dimentional array

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

    > 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

  3. #3


    ekkehard.horner Guest

    Re: Randomize a one dimentional array

    Bob Smith schrieb:

    > 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

  4. #4


    ekkehard.horner Guest

    Re: Randomize a one dimentional array

    Auric__ schrieb:

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

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

    > 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

  5. #5


    Bob Smith Guest

    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:

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

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

  6. #6


    Auric__ Guest

    Re: Randomize a one dimentional array

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

    > Auric__ schrieb:

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

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

  7. #7


    Auric__ Guest

    Re: Randomize a one dimentional array

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

    > 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

  8. #8


    ekkehard.horner Guest

    Re: Randomize a one dimentional array

    Auric__ schrieb:

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

      My System SpecsSystem Spec

  9. #9


    ekkehard.horner Guest

    Re: Randomize a one dimentional array

    ekkehard.horner schrieb:

    > Auric__ schrieb:

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

      My System SpecsSystem Spec

  10. #10


    Auric__ Guest

    Re: Randomize a one dimentional array

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

    > ekkehard.horner schrieb:

    >> Auric__ schrieb:

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

Page 1 of 2 12 LastLast
Randomize a one dimentional array problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fast copy method of sub array (=array range) possible? Thomas Lebrecht VB Script 6 19 Mar 2009
Search a Two-Dimentional Array Bob Smith VB Script 4 24 Nov 2008
Stupid Array Tricks: Initializing an Array to a Certain Size tojo2000 PowerShell 2 09 Sep 2008
How to Randomize characters in a string RICK VB Script 17 22 Aug 2008
how to assign values to array and how to create array via variable Frank PowerShell 1 13 Mar 2007