Help with generate a pair of random number

Hi,

Is anybody experience generate a pair of random number by using awk command?
I wanna to generate a pair of random number (range from 1 to 4124) and repeats it 416 times.

Desired output

2 326
123 1256
341 14
3245 645
.
.
.

I did write the below command:

awk "BEGIN{srand($RANDOM); for(i=1;i<416;i++)print int(1+ rand()*4124)}"
1013
4078
2606
2086
1019
373
.
.

But it only able to generate a single random number (range from 1 to 4124) and repeats it 416 times.

I wish to generate a pair of random number (range from 1 to 4124) and repeats it 416 times.
Thanks for any advice.

Hello perl_beginner,

Could you please try following and let me know if this helps you.

awk 'BEGIN{j=4124;for(i=1;i<=4124;i++){A=i;B=j;j--};for(k in A){print A[k] OFS B[k];c++;if(c>=416){exit}}}'

Code above will show output of both columns differently but one shouldn't expect like each time output will be different from previous run of command, hope it helps. If you have any queries or you have further requirement request you to please provide in details statement.

Thanks,
R. Singh

1 Like

OSX 10.7.5, default terminal using 'sh'.
Try:-

#/bin/sh
for n in {1..416}
do
	echo "$(( ( $RANDOM % 4124 ) + 1 )) $(( ( $RANDOM % 4124 ) + 1 ))"
done
1 Like

You're not too far off! Try

awk "BEGIN{srand($RANDOM); for(i=1;i<416;i++)print int(1+ rand()*4124), int(1+rand()*4124)}"
1931 3259
1456 1559
150 357
.
.
.
1 Like

You guy are awesome and very experience in coding :slight_smile:
Many thanks.

It really help me a lot ^.^

---------- Post updated at 09:12 AM ---------- Previous update was at 09:12 AM ----------

Awesome code :slight_smile:

---------- Post updated at 09:12 AM ---------- Previous update was at 09:12 AM ----------

It worked fine :slight_smile:
Hehe.

---------- Post updated at 09:13 AM ---------- Previous update was at 09:12 AM ----------

Thanks.
It is fantastic :slight_smile:

In case you would like to do something similar in Perl.

perl -le 'for(1..416){printf "%4d %4d\n", int(rand(4124))+1, int(rand(4124))+1}'
1046  261
  16 2900
2676  494
1137 2638
   6 3165
3141  584
  73 2299
3430   70
2056 2279
1343 3486
2688  274
2582 3591
 699  411

Added a bit of formatting