Asked  7 Months ago    Answers:  5   Viewed   33 times

I'm currently using the code below to try and read a random line from random.txt and pass it as $data, however it comes back empty. Each string has its own line, am I missing something here? Shouldn't this work? If not how can I get a random line from my text file and use it as my $data string?

$f_contents = file("random.txt");
$line = $f_contents[array_rand($f_contents)];
$data = $line;

Solved - Bad CHMOD Thought I had double checked it, sorry to post a question.

 Answers

54

Make sure your file has read permissions set, should be CHMOD'd to 644 or 744.

Wednesday, March 31, 2021
 
tiny
answered 7 Months ago
62

Exactly like you described it in words: By increment it with a random number between 1 and 5.

while ($i < 1000) {
  echo $i;
  $i += rand(1,5);
}

rand()

Saturday, May 29, 2021
 
jerrygarciuh
answered 5 Months ago
10

One way with sed:

sed 's/$/p/' linesfile | sed -n -f - datafile

You can use the same trick with awk:

sed 's/^/NR==/' linesfile | awk -f - datafile

Edit - Huge files alternative

With regards to huge number of lines it is not prudent to keep whole files in memory. The solution in that case can be to sort the numbers-file and read one line at a time. The following has been tested with GNU awk:

extract.awk

BEGIN {
  getline n < linesfile
  if(length(ERRNO)) {
    print "Unable to open linesfile '" linesfile "': " ERRNO > "/dev/stderr"
    exit
  }
}

NR == n { 
  print
  if(!(getline n < linesfile)) {
    if(length(ERRNO))
      print "Unable to open linesfile '" linesfile "': " ERRNO > "/dev/stderr"
    exit
  }
}

Run it like this:

awk -v linesfile=$linesfile -f extract.awk infile

Testing:

echo "2
4
7
8
10
13" | awk -v linesfile=/dev/stdin -f extract.awk <(paste <(seq 50e3) <(seq 50e3 | tac))

Output:

2   49999
4   49997
7   49994
8   49993
10  49991
13  49988
Friday, June 25, 2021
 
cegfault
answered 4 Months ago
83

You can use array_multisort to order the array values by a second array of mt_rand values:

$arr = array(1,2,3,4,5,6);

mt_srand('123');
$order = array_map(create_function('$val', 'return mt_rand();'), range(1, count($arr)));
array_multisort($order, $arr);

var_dump($arr);

Here $order is an array of mt_rand values of the same length as $arr. array_multisort sorts the values of $order and orders the elements of $arr according to the order of the values of $order.

Friday, July 16, 2021
 
Rocket
answered 4 Months ago
84

if you have that many lines, are you sure you want exactly 1% or a statistical estimate would be enough?

In that second case, just randomize at 1% at each line...

awk 'BEGIN {srand()} !/^$/ { if (rand() <= .01) print $0}'

If you'd like the header line plus a random sample of lines after, use:

awk 'BEGIN {srand()} !/^$/ { if (rand() <= .01 || FNR==1) print $0}'
Tuesday, July 27, 2021
 
o_flyer
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :