LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-31-2020, 06:52 AM   #1
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 369
Blog Entries: 1

Rep: Reputation: 49
sort -R or shuf ?


Dear LQ,

What is your preferred way of sorting randomly, sort -R or shuf ?
 
Old 12-31-2020, 06:59 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
sort -R works differently, read man page about that. So use what you really need.
 
1 members found this post helpful.
Old 12-31-2020, 07:21 AM   #3
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 369

Original Poster
Blog Entries: 1

Rep: Reputation: 49
sort by random hash of key... I don't even know what's
that supposed to mean ? I never thought of a hash as
possibly being random, I thought they were
deterministic.
 
1 members found this post helpful.
Old 12-31-2020, 07:26 AM   #4
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by ychaouche View Post
sort by random hash of key...
Where are you getting this? Both my systems' 'sort' man pages say this:
Code:
  -R, --random-sort
          shuffle, but group identical keys.
That is obviously different from just "shuffle".
 
Old 12-31-2020, 09:00 AM   #5
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 369

Original Poster
Blog Entries: 1

Rep: Reputation: 49
Code:
ychaouche#ychaouche-PC 15:57:56 ~/DOWNLOADS/APPS/THUNDERBIRD/thunderbird $ sort --version
sort (GNU coreutils) 8.21
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and Paul Eggert.
ychaouche#ychaouche-PC 15:58:57 ~/DOWNLOADS/APPS/THUNDERBIRD/thunderbird $ sort --help | grep '\-R'
  -R, --random-sort           sort by random hash of keys
                                numeric -n, random -R, version -V
ychaouche#ychaouche-PC 16:00:15 ~/DOWNLOADS/APPS/THUNDERBIRD/thunderbird $ man --pager=cat sort | grep '\-R' -A1
       -R, --random-sort
              sort by random hash of keys
--
              sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V

ychaouche#ychaouche-PC 16:00:40 ~/DOWNLOADS/APPS/THUNDERBIRD/thunderbird $
 
Old 12-31-2020, 09:12 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Sadly, the definitive documentation for GNU tools are not man pages, but info manuals. For sort -R, it says:
Quote:
-R
--random-sort
--sort=random
Sort by hashing the input keys and then sorting the hash values. Choose the hash function at random, ensuring that it is free of collisions so that differing keys have differing hash values. This is like a random permutation of the inputs (see shuf invocation), except that keys with the same value sort together.

If multiple random sort fields are specified, the same random hash function is used for all fields. To use different random hash functions for different fields, you can invoke sort more than once.

The choice of hash function is affected by the --random-source option.
 
Old 12-31-2020, 12:23 PM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
That is more or less identical, but you are right, this info manual is better - although sort by those hashes means really grouping by identical keys.
And also means this is reproducible, so the sort will give always the same "random" result with the same hashing algorithm (see --random-source=FILE).

It is still different from the real shuffle.
 
Old 01-03-2021, 04:33 AM   #8
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 369

Original Poster
Blog Entries: 1

Rep: Reputation: 49
So shuffle is more random ?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Linux shuf Command Tutorial for Beginners (with Examples) LXer Syndicated Linux News 0 11-14-2018 12:50 AM
Does 'shuf' need to be reseeded? fiascoburnett Linux - General 2 08-22-2016 12:41 AM
[SOLVED] Find with shuf to cp with backup zakur0 Linux - Newbie 1 07-26-2016 04:54 AM
[SOLVED] Does 'shuf' seed itself with a zero? Dafydd Linux - General 2 10-26-2013 04:35 PM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 08:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration