Re: Needing a random number generator for scripting

2001-09-28 Thread Rob Hudson
Check out 'man perlfaq4', and the question titled, How do I shuffle
an array randomly?.  It gives a nice algorithm for shuffling, which
could be applied similar to below to output shuffled lines.

-Rob

 On 20010928.0900, Anthony J. Breeds-Taurima said ...

 On Thu, 27 Sep 2001, Joey Hess wrote:
 
  Anthony J. Breeds-Taurima wrote:
   == randomize ==
   #!/usr/bin/perl
   ## print a file in random order
   while() {
 $_{$_}=0
   }
   foreach (keys%_) {
 print
   }
 
  keys is hardly random. It always outputs the keys in the same order for
  a given set of keys.
 
 I didn't say it was _good_ I just said I used it.
 
 == randomize2 ==
 #!/usr/bin/perl
 ## print a file in random order
 while() {
  $_{$_}=rand;
 }
 foreach (sort {$_{$a} = $_{$b}} (keys%_)) {
 print
 }
 
 This is now basically the awk|sed|cut solution just in perl.
 
 Yours Tony.
 
 /*
  * The significant problems we face cannot be solved at the
  * same level of thinking we were at when we created them.
  * --Albert Einstein
  */
 
 
 -- 
 To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
 with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]
 
 



Re: Needing a random number generator for scripting

2001-09-27 Thread Dave Thayer
On Thu, Sep 27, 2001 at 08:45:57PM -0500, Jeremy Whetzel wrote:
 
 I'm in the process of writing up a script that I need to be able to
 randomly switch around the lines in a text file.  (ala a random mp3
 playlist) Would anyone have any suggestions?  I was originally thinking
 of using a random number generator for it, but if there's a tool that
 would work better...?
 
Here's what I use:

awk 'BEGIN {srand()} {print rand() \t $0 }' unshuffled.m3u \
 | sort | cut -f2  shuffled.m3u

The way this works is that the awk command prepends a random number to
each line and pipes the lines into sort which sorts by the random
numbers. Next, cut is used to remove the random numbers and you get
shuffled output.

HTH

dt.

-- 
Dave Thayer   | If trees could scream, would we be so cavalier about
Denver, Colorado USA  | cutting them down? We might, if they screamed all
[EMAIL PROTECTED] | the time, for no good reason. - Jack Handey



Re: Needing a random number generator for scripting

2001-09-27 Thread Anthony J. Breeds-Taurima
On Thu, 27 Sep 2001, Dave Thayer wrote:

 On Thu, Sep 27, 2001 at 08:45:57PM -0500, Jeremy Whetzel wrote:
 
  I'm in the process of writing up a script that I need to be able to
  randomly switch around the lines in a text file.  (ala a random mp3
  playlist) Would anyone have any suggestions?  I was originally thinking
  of using a random number generator for it, but if there's a tool that
  would work better...?
 
 Here's what I use:

 awk 'BEGIN {srand()} {print rand() \t $0 }' unshuffled.m3u \
  | sort | cut -f2  shuffled.m3u

Here are 2 I use:
== randline ==
#!/usr/bin/perl
## print 1 randomline from a file
@_=;
print $_[rand$#_];

== randomize ==
#!/usr/bin/perl
## print a file in random order
while() {
$_{$_}=0
}
foreach (keys%_) {
print
}


ie
~/bin$ cat ~/Playlists/Karma
/home/tony/.tmp/Delerium/Karma_Disk_1/01_Enchanted.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/02_Duende.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/03_Twilight.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/04_Silence.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/05_Forgotten_Worlds.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/06_Lamentation.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/07_Euphoria_firefly.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/08_Remembrance.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/09_Wisdom.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/10_Window_To_Your_Soul.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/11_Til_The_End_Of_Time.mp3
/home/tony/.tmp/Delerium/Karma_Disk_2/01_silence_sanctuary_mix.mp3
/home/tony/.tmp/Delerium/Karma_Disk_2/02_euphoria_firefly_rabbit_in_the_moons_divine_gothic_disco_mix.mp3
/home/tony/.tmp/Delerium/Karma_Disk_2/03_flowers_become_screens_frequency_modulation_mix.mp3
/home/tony/.tmp/Delerium/Karma_Disk_2/04_incantation_12_mix_edit.mp3
/home/tony/.tmp/Delerium/Karma_Disk_2/05_duende_bleak_desolation_mix.mp3
/home/tony/.tmp/Delerium/Karma_Disk_2/06_heavens_earth.mp3

~/bin$ randline ~/Playlists/Karma
/home/tony/.tmp/Delerium/Karma_Disk_1/05_Forgotten_Worlds.mp3

~/bin$ randline ~/Playlists/Karma
/home/tony/.tmp/Delerium/Karma_Disk_1/01_Enchanted.mp3

~/bin$ randomize ~/Playlists/Karma
/home/tony/.tmp/Delerium/Karma_Disk_2/04_incantation_12_mix_edit.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/06_Lamentation.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/11_Til_The_End_Of_Time.mp3
/home/tony/.tmp/Delerium/Karma_Disk_2/06_heavens_earth.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/07_Euphoria_firefly.mp3
/home/tony/.tmp/Delerium/Karma_Disk_2/05_duende_bleak_desolation_mix.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/05_Forgotten_Worlds.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/04_Silence.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/02_Duende.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/08_Remembrance.mp3
/home/tony/.tmp/Delerium/Karma_Disk_2/03_flowers_become_screens_frequency_modulation_mix.mp3
/home/tony/.tmp/Delerium/Karma_Disk_2/01_silence_sanctuary_mix.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/10_Window_To_Your_Soul.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/01_Enchanted.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/09_Wisdom.mp3
/home/tony/.tmp/Delerium/Karma_Disk_2/02_euphoria_firefly_rabbit_in_the_moons_divine_gothic_disco_mix.mp3
/home/tony/.tmp/Delerium/Karma_Disk_1/03_Twilight.mp3

Yours Tony.

/*
 * The significant problems we face cannot be solved at the
 * same level of thinking we were at when we created them.
 * --Albert Einstein
 */



Re: Needing a random number generator for scripting

2001-09-27 Thread A.R. \(Tom\) Peters
On 27 Sep 2001, Jeremy Whetzel wrote:

 I'm in the process of writing up a script that I need to be able to
 randomly switch around the lines in a text file.  (ala a random mp3
 playlist) Would anyone have any suggestions?  I was originally thinking
 of using a random number generator for it, but if there's a tool that
 would work better...?

/dev/random gives random bits.  I don't know where it is documented.
There is a system call random() (see man 3 random).  You could write a
wrapper C program to use it in scripts.

--
#!$!%(@^%#%*(([EMAIL 
PROTECTED]@^$##*#@(%)@**$!(!^(#((#%!)%*@)($($$%(@#)*!^$)[EMAIL PROTECTED]@)

Tom thriving on chaos Peters
NL-1062 KD nr 149   tel.+31-204080204
Amsterdam   e-mail  [EMAIL PROTECTED]



Re: Needing a random number generator for scripting

2001-09-27 Thread dman
On Thu, Sep 27, 2001 at 09:00:26AM +0200, A.R. (Tom) Peters wrote:
| On 27 Sep 2001, Jeremy Whetzel wrote:
| 
|  I'm in the process of writing up a script that I need to be able to
|  randomly switch around the lines in a text file.  (ala a random mp3
|  playlist) Would anyone have any suggestions?  I was originally thinking
|  of using a random number generator for it, but if there's a tool that
|  would work better...?
| 
| /dev/random gives random bits.  I don't know where it is documented.
| There is a system call random() (see man 3 random).  You could write a
| wrapper C program to use it in scripts.

Naw, just use python :-).


#!/usr/bin/env python

def my_randint() :

This is a custom randomizer that uses the Linux /dev/random.
It returns a random integer in an unknown range.  (Use modulo
division to scale it down to the intended range)

# used to convert the raw bits to a Python int
import struct

dev_random = open( /dev/random , r )
# use a 4-byte integer
raw_index = dev_random.read( 4 )
# 'unsigned int' however this returns a Python Long
return struct.unpack( I , raw_index )[0]


Or you can use the standard built-in random functions :

#!/usr/bin/env python
import random
print random.random() # this gives a float between 0 and 1
print random.randint( 0 , 10 ) # this gives an int between 0 and 10
   # (the arguments)


-D



Re: Needing a random number generator for scripting

2001-09-27 Thread Noah Meyerhans
On Thu, Sep 27, 2001 at 09:20:04AM -0400, dman wrote:
 | /dev/random gives random bits.  I don't know where it is documented.
 | There is a system call random() (see man 3 random).  You could write a
 | wrapper C program to use it in scripts.
 
 Naw, just use python :-).
 
 
 #!/usr/bin/env python
 
 def my_randint() :

snip

 #!/usr/bin/env python
 import random
 print random.random() # this gives a float between 0 and 1
 print random.randint( 0 , 10 ) # this gives an int between 0 and 10
# (the arguments)
 

Why do you people insist on doing things the hard way?  'echo $RANDOM'
in bash works as well as any of these solutions posted here...  
'echo $(($RANDOM % $randmax))' where randmax is whatever you want the
highest random value to be works, too.

noah

-- 
 ___
| Web: http://web.morgul.net/~frodo/
| PGP Public Key: http://web.morgul.net/~frodo/mail.html 


pgpBY7LV27OXk.pgp
Description: PGP signature


Re: Needing a random number generator for scripting

2001-09-27 Thread Vineet Kumar
* Anthony J. Breeds-Taurima ([EMAIL PROTECTED]) [010926 23:14]:
 == randline ==
 #!/usr/bin/perl
 ## print 1 randomline from a file
 @_=;
 print $_[rand$#_];

while this one works, the one Martin posted is a better solution.
They're both O(n) in terms of time, but Martin's is O(1) wrt space,
whereas this one is Also O(n) -- it reads in and stores the entire file
and then picks out a line. Unfortunately, it doesn't lend itself as well
to extension to the case of shuffling a file. But for one random line
from a file, it's a very elegant solution. See the archives for more
discussion.

-- 
Vineet   http://www.anti-dmca.org
Unauthorized use of this .sig may constitute violation of US law.
echo Qba\'g gernq ba zr\! |tr 'a-zA-Z' 'n-za-mN-ZA-M'


pgpA8470FqfIh.pgp
Description: PGP signature


Re: Needing a random number generator for scripting

2001-09-27 Thread Raja R Harinath
Hi,

Jeremy Whetzel [EMAIL PROTECTED] writes:

 I'm in the process of writing up a script that I need to be able to
 randomly switch around the lines in a text file.  (ala a random mp3
 playlist) Would anyone have any suggestions?  I was originally thinking
 of using a random number generator for it, but if there's a tool that
 would work better...?

If you have 'bash', you can use:

  # scale should be larger than number of lines
  scale=1000

  # number of lines in file

  ## if the file is fixed, you can hardcode this value
  ## otherwise, if the format has a count on the first line (like
  ##fonts.dir), you can use
  ##  lines=`head -1 file`
  ## and change the '1' in the dc command below to '2'.

  lines=`wc -l file`

  # pick a random line
  line=`echo $RANDOM $scale * 32768 / $lines * $scale / 1 + p | dc`

  # print only that line
  sed -n ${line}p file

- Hari
-- 
Raja R Harinath -- [EMAIL PROTECTED]
When all else fails, read the instructions.  -- Cahn's Axiom
Our policy is, when in doubt, do the right thing.   -- Roy L Ash



Re: Needing a random number generator for scripting

2001-09-27 Thread dman
On Thu, Sep 27, 2001 at 10:55:50AM -0400, Noah Meyerhans wrote:
| On Thu, Sep 27, 2001 at 09:20:04AM -0400, dman wrote:
|  | /dev/random gives random bits.  I don't know where it is documented.
|  | There is a system call random() (see man 3 random).  You could write a
|  | wrapper C program to use it in scripts.
|  
|  Naw, just use python :-).
|  
|  #!/usr/bin/env python
|  
|  def my_randint() :
| 
| snip
| 
|  #!/usr/bin/env python
|  import random
|  print random.random() # this gives a float between 0 and 1
|  print random.randint( 0 , 10 ) # this gives an int between 0 and 10
| # (the arguments)
| 
| Why do you people insist on doing things the hard way?  'echo $RANDOM'
| in bash works as well as any of these solutions posted here...  

I didn't know bash provided a way to get a random number.  Thanks!

-D



Re: Needing a random number generator for scripting

2001-09-27 Thread Rich Puhek
Dave gets my vote for the best answer.

I'm a bit biased in that I like a meat grinder approach with sed, awk,
cat, and a lot of pipesigns, but you've got to admit, his little
oneliner script is much tidier than the here's how it's done in my
favorite language answers.



Dave Thayer wrote:
 
 awk 'BEGIN {srand()} {print rand() \t $0 }' unshuffled.m3u \
  | sort | cut -f2  shuffled.m3u
 

_
 
Rich Puhek   
ETN Systems Inc. 
_



Re: Needing a random number generator for scripting

2001-09-27 Thread Joey Hess
Anthony J. Breeds-Taurima wrote:
 == randomize ==
 #!/usr/bin/perl
 ## print a file in random order
 while() {
   $_{$_}=0
 }
 foreach (keys%_) {
   print
 }

keys is hardly random. It always outputs the keys in the same order for
a given set of keys.

-- 
see shy jo



Re: Needing a random number generator for scripting

2001-09-27 Thread Anthony J. Breeds-Taurima
On Thu, 27 Sep 2001, Joey Hess wrote:

 Anthony J. Breeds-Taurima wrote:
  == randomize ==
  #!/usr/bin/perl
  ## print a file in random order
  while() {
  $_{$_}=0
  }
  foreach (keys%_) {
  print
  }

 keys is hardly random. It always outputs the keys in the same order for
 a given set of keys.

I didn't say it was _good_ I just said I used it.

== randomize2 ==
#!/usr/bin/perl
## print a file in random order
while() {
 $_{$_}=rand;
}
foreach (sort {$_{$a} = $_{$b}} (keys%_)) {
print
}

This is now basically the awk|sed|cut solution just in perl.

Yours Tony.

/*
 * The significant problems we face cannot be solved at the
 * same level of thinking we were at when we created them.
 * --Albert Einstein
 */



Re: Needing a random number generator for scripting

2001-09-27 Thread Jeremy

I want to thank you all for your help.  Many of the solutions sounded great.
(Makes me wish I knew a bit about some of the other programming languages
available, but I'm strictly a beginner, so I'm sticking with bash at the
moment). But it's good to know that there's such a knowledgable group out
there that's also very willing to help.  =0)

Thanks again,
Jeremy



Needing a random number generator for scripting

2001-09-26 Thread Jeremy Whetzel

I'm in the process of writing up a script that I need to be able to
randomly switch around the lines in a text file.  (ala a random mp3
playlist) Would anyone have any suggestions?  I was originally thinking
of using a random number generator for it, but if there's a tool that
would work better...?

Thanks,
Jeremy



Re: Needing a random number generator for scripting

2001-09-26 Thread Martin F Krafft
cat list | \
  perl -e 'srand; rand($.)  1  ($line = $_) while ; print $line;'

-- as suggested on this list earlier, it will return a random line out
of the file list. if you want the entire list to be shuffled, then
search the archives for random lines and a post by me. in it's
replies, there is a permutator of input files.

martin;  (greetings from the heart of the sun.)
  \ echo mailto: !#^.*|tr * mailto:; [EMAIL PROTECTED]
-- 
a good scapegoat is nearly as welcome as a solution to the problem.


pgpMvB79VzrZk.pgp
Description: PGP signature