Help with macro to scramble words

Hello! This is my first post.
I am a new user to LibreOffice and I am enjoying it so far!
I created an Excel sheet in the past with a macro to help scramble my daughter’s spelling words so that she can unscramble them as part of her studying. However, something isn’t translating from Excel to Calc. My resulting words are scrambled but they do not contain the same letters as the original word.
Example: cell F2:H2 shows “backpack”; cell A22 displays “cacannduud”. It should display something like “kpbkacac”
Below is my Function

Function Scramble(oldname)
    On Error Resume Next
    n = Len(oldname)
    newname = ""
    Do
        i = Int(Rnd() * n) + 1
        c = Mid(oldname, i, 1)
        If c <> "*" Then
            newname = newname & c
            oldname = Replace(oldname, c, "*", , 1)
        End If
    Loop Until Len(newname) = n
    Scramble = LCase(newname)
End Function

Try change
oldname = Replace(oldname, c, "*", , 1)
to
oldname = Replace(oldname, c, "*", 1, 1)