Shuffle a string

This topic contains 6 replies, has 6 voices, and was last updated by  Joel Sallow 2 days, 3 hours ago.

  • Author
    Posts
  • #68641

    Mark Prior
    Participant

    Hopefully easy 🙂

    
    $string = "12345678"
    
    

    how do i shuffle said sting to produce a random arrangement ? i.e numbers come out as 153264987?

  • #68643

    Olaf Soyk
    Participant
    ($string -split '' | Sort-Object {Get-Random}) -join ''
  • #68646

    Mark Prior
    Participant

    Thanks Olaf

  • #68709

    Max Kozlov
    Participant

    oneliner contest ? 🙂
    ($string.ToCharArray() | Get-Random -Count $string.Length) -join "

  • #102700

    RShambo
    Participant

    Olaf and Max

    Thank you!! I was thinking I would need to loop through the string I needed to shuffle. You guys rock!

  • #102718

    Rob Simmers
    Participant

    Depending on what you are doing, another option may be a range as well:

    PS C:\WINDOWS\system32> Get-Random -Minimum 100000000 -Maximum 199999999
    156910726
    
    PS C:\WINDOWS\system32> Get-Random -Minimum 100000000 -Maximum 199999999
    170770366
    
    PS C:\WINDOWS\system32> Get-Random -Minimum 100000000 -Maximum 199999999
    169781938
    
  • #102721

    Joel Sallow
    Participant

    Here for the oneliner contest! 😉

    -join($string -split''|sort{Get-Random})

    39 characters. 😀

You must be logged in to reply to this topic.