Off Topic, but here are more clever people ;-) I am lookinig for way to "unsort" a list of numbers!!! E.g., I have a list of numbers, each in a line, but I don't want them in order, it should randomly! Any hints how to do that. And yes, it must be this numbers and not new numbers ;-) bye Ronald Ronald Wiplinger (ÃQ¤¯¯Ç), CEO, ELMIT - The Solution Provider Tel. +886 2 8809-7980, Fax. +886 2 2809-0183, Mobile: +886 915 653-452 Net2Phone:8869550066, ICQ: 111651169 http://www.elmit.com http://www.wiplinger.org
On 12-Oct-01 Ronald Wiplinger wrote:
Off Topic, but here are more clever people ;-)
I am lookinig for way to "unsort" a list of numbers!!!
E.g., I have a list of numbers, each in a line, but I don't want them in order, it should randomly!
Any hints how to do that. And yes, it must be this numbers and not new numbers ;-)
If you want a quick one-off one-liner, the following does it nicely:
Suppose your file, with a separate original number in each line,
is called "temp". Then:
cat temp |
awk 'BEGIN{srand()} {$2=$1; $1=rand(); print $0}' |
sort -n |
awk '{print $2}'
will do it. This basically outputs the original lines in
a random order. (In fact, they don't need to be numbers
thnough, as written, the above will fail if they are text
lines with spaces since the default field separator in awk
is the space; however, if needed, this can be worked round
by changing awk's internal FS and OFS variables in the "awk"
invocations).
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding)
Hi, On 12-Oct-01 Ted Harding wrote:
If you want a quick one-off one-liner, the following does it nicely:
Suppose your file, with a separate original number in each line, is called "temp". Then:
cat temp | awk 'BEGIN{srand()} {$2=$1; $1=rand(); print $0}' | sort -n | awk '{print $2}'
will do it. This basically outputs the original lines in a random order. (In fact, they don't need to be numbers thnough, as written, the above will fail if they are text lines with spaces since the default field separator in awk is the space; however, if needed, this can be worked round by changing awk's internal FS and OFS variables in the "awk" invocations).
Following the above, I've extended the method along the lines
suggested. The following two scripts will:
(a) output the input file with the lines rearranged in random order,
whether these lines are numbers or text;
(b) make a random selection of a given number out of the lines
in the file.
Copy the scripts into the named files and make them executable
(chmod 755).
(a) rand_perm_lines
#! /bin/bash
cat $1 |
awk 'BEGIN{FS=SUBSEP; OFS=SUBSEP; srand()} \
{$2=$1; $1=rand(); print $0}' |
sort -n |
awk 'BEGIN{FS=SUBSEP;OFS=" "}{print $2}'
(b) rand_select_lines
#! /bin/bash
cat $1 |
awk 'BEGIN{FS=SUBSEP; OFS=SUBSEP; srand()} \
{$2=$1; $1=rand(); print $0}' |
sort -n |
awk 'BEGIN{FS=SUBSEP;OFS=" "}{print $2}' |
head -n $2
(note that (b) is simply (a) with a bit tagged on).
Usage:
(a) rand_perm_lines filename
(b) rand_select_lines filename N
(where N is the number of lines you want to select).
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding)
The following C++ code will do it. Save text as unsort.cc and compile g++ -o unsort unsort.cc Then, if numbers are in a file list unsort < list to "unsort" unsort < list > file to "unsort" and put in a file. #include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { vector<double> numbers; copy( istream_iterator<double>( cin ), istream_iterator<double>(), back_inserter( numbers ) ); random_shuffle( numbers.begin(), numbers.end() ); copy( numbers.begin(), numbers.end(), ostream_iterator<double>( cout, "\n") ); return 0; } You may want to add something to the code to scramble the random number stream befor you do he random shuffle. Alternatively, you might see if perl has a quick way of doing this. JDL Ronald Wiplinger wrote:
Off Topic, but here are more clever people ;-)
I am lookinig for way to "unsort" a list of numbers!!!
E.g., I have a list of numbers, each in a line, but I don't want them in order, it should randomly!
Any hints how to do that. And yes, it must be this numbers and not new numbers ;-)
bye
Ronald
Ronald Wiplinger (ÃQ¤¯¯Ç), CEO, ELMIT - The Solution Provider Tel. +886 2 8809-7980, Fax. +886 2 2809-0183, Mobile: +886 915 653-452
Net2Phone:8869550066, ICQ: 111651169 http://www.elmit.com http://www.wiplinger.org
Hi, My other half is about to get a laptop and wants dual Linux/Windows on it. I already have a dual boot laptop and during the process of sorting it out discovered that SuSE 7.1 can shrink Win98, but not Win2K. So the question is, which of Window 98/ME/NT4/"K can SuSE 7.1 (and 7.3 if that info is available) shrink the partitions on? Thanks in anticipation of help :) alan
participants (4)
-
Alan Lenton
-
John D Lamb
-
Ronald Wiplinger
-
Ted.Harding@nessie.mcc.ac.uk