Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

PowerShell out-random function

Bruno left a comment on the post about my solution for Scripting Games 2008 Advanced Windows PowerShell Event 7 about my sort routine, using a scriptblock could make it even simpler, and so elegant I decided to make an extra post about it, a generic randomize function in PowerShell can be as easy as :

  function out-random {
   $r = New-Object random
   $input | sort {$r.next()}
}

Sample usage :


PS C:\scripts> 1..10 | out-random
7
6
8
10
9
2
1
3
5
4

The Posh life can be so simple sometimes. 

Enjoy,

Greetings /\/\o\/\/

Published Thursday, February 28, 2008 7:33 PM by MoW

Comments

# re: PowerShell out-random function

The coolest thing about the Scripting Games is the exchange of information and seeing how some of the greatest scripters out there craft their solutions. I've been learning a lot, personally.

Just a minor correction in the out-random function: you forgot the () in $r.next(). If the next() method doesn't actually get called, you will get the same output each time around. =)

Thursday, February 28, 2008 8:14 PM by Bruno Gomes

# Scripting Games: Advanced PowerShell - Event 1 « PowerShell ?? ???????????? ??????????????

# re: PowerShell out-random function

@ Bruno,

Copy paste mistake, I fixed it.

Thanks again,

Greetings /\/\o\/\/

Friday, February 29, 2008 8:14 AM by MoW
Anonymous comments are disabled