Shuffle a string

Welcome Forums General PowerShell Q&A Shuffle a string

This topic contains 6 replies, has 6 voices, and was last updated by

 
Participant
10 months, 1 week ago.

  • Author
    Posts
  • #68641

    Participant
    Topics: 47
    Replies: 156
    Points: 42
    Rank: Member

    Hopefully easy 🙂

    
    $string = "12345678"
    
    

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

  • #68643

    Participant
    Topics: 1
    Replies: 1267
    Points: 1,395
    Helping Hand
    Rank: Community Hero
    ($string -split '' | Sort-Object {Get-Random}) -join ''
  • #68646

    Participant
    Topics: 47
    Replies: 156
    Points: 42
    Rank: Member

    Thanks Olaf

  • #68709

    Participant
    Topics: 2
    Replies: 376
    Points: 0
    Rank: Member

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

  • #102700

    Participant
    Topics: 0
    Replies: 6
    Points: 0
    Rank: Member

    Olaf and Max

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

  • #102718

    Participant
    Topics: 8
    Replies: 1190
    Points: 639
    Helping Hand
    Rank: Major Contributor

    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

    Participant
    Topics: 2
    Replies: 399
    Points: 697
    Helping Hand
    Rank: Major Contributor

    Here for the oneliner contest! 😉

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

    39 characters. 😀

The topic ‘Shuffle a string’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort