Vbscript 随机重新排列单词中的字母

Vbscript 随机重新排列单词中的字母,vbscript,asp-classic,Vbscript,Asp Classic,以本SO问题/答案为起点: 我有一个简单的代码,可以把一个单词拆分成单个字母: <% Dim word1, i word1 = "particle" For i = 1 To Len(word1) Response.Write "<p>" & Mid(word1, i, 1) & "</p>" Next %> 这是我想要实现的一个例子: 然而,我意识到这说起来容易做起来难 我

以本SO问题/答案为起点:

我有一个简单的代码,可以把一个单词拆分成单个字母:

<%
Dim word1, i
word1 = "particle"
For i = 1 To Len(word1)
    Response.Write "<p>" & Mid(word1, i, 1) & "</p>"
Next
%>
这是我想要实现的一个例子:

然而,我意识到这说起来容易做起来难

我想知道是否有人对如何使用经典ASP实现这一点有任何建议


谢谢,这里有一种方法:

Function ShuffleText(p_sText)
    Dim iLength
    Dim iIndex
    Dim iCounter
    Dim sLetter
    Dim sText
    Dim sShuffledText
    
    ' Copy text passed as parameter
    sText = p_sText
    
    ' Get text length
    iLength = Len(sText)
    
    For iCounter = iLength To 1 Step -1
        
        ' Get random index
        iIndex = 1 + Int(Rnd * (iCounter))
        
        ' Get character at that index
        sLetter = Mid(sText, iIndex, 1)
        
        ' Remove character from string
        sText = Left(sText, iIndex - 1) & Mid(sText, iIndex + 1)
        
        ' Add character to shuffled string
        sShuffledText = sShuffledText & sLetter
            
    Next

    ' Return shuffled text
    ShuffleText = sShuffledText
    
End Function
这段代码在字符串中选择一个随机字符,将其删除并添加到一个无序字符串中。它将重复此过程,直到完成所有字符


可能有更有效的方法可以做到这一点,首先将一组数字随机化,并将这些数字用作
iIndex
,而无需操纵
sText
字符串。

非常感谢埃蒂安,太好了。我非常羡慕你的编程逻辑技巧。再次感谢!
Function ShuffleText(p_sText)
    Dim iLength
    Dim iIndex
    Dim iCounter
    Dim sLetter
    Dim sText
    Dim sShuffledText
    
    ' Copy text passed as parameter
    sText = p_sText
    
    ' Get text length
    iLength = Len(sText)
    
    For iCounter = iLength To 1 Step -1
        
        ' Get random index
        iIndex = 1 + Int(Rnd * (iCounter))
        
        ' Get character at that index
        sLetter = Mid(sText, iIndex, 1)
        
        ' Remove character from string
        sText = Left(sText, iIndex - 1) & Mid(sText, iIndex + 1)
        
        ' Add character to shuffled string
        sShuffledText = sShuffledText & sLetter
            
    Next

    ' Return shuffled text
    ShuffleText = sShuffledText
    
End Function