Generate a random string where the first character is always "R"

andrew_s_2022

New Member
Joined
Nov 22, 2022
Messages
3
Reaction score
1
Credits
30
I am generating 50 random strings, each 11-character long by running:

cat /dev/urandom | LC_ALL=C tr -dc 'A-Z' | fold -w 11 | head -n 50

Now, how could I constrain the first character to always be letter R?

RHFEKGJTUSJW
RJGHSGWNGHT
RJGHTYWERRT
RTHNBMVUFGH
RLOPOPITYWV
.....

Thanks for any hint
 


I am generating 50 random strings, each 11-character long by running:

cat /dev/urandom | LC_ALL=C tr -dc 'A-Z' | fold -w 11 | head -n 50

Now, how could I constrain the first character to always be letter R?

RHFEKGJTUSJW
RJGHSGWNGHT
RJGHTYWERRT
RTHNBMVUFGH
RLOPOPITYWV
.....

Thanks for any hint

Would this work?
Bash:
while read line; do
  echo "R$line"
done << cat /dev/urandom | LC_ALL=C tr -dc 'A-Z' | fold -w 10 | head -n 50

Assuming I haven’t made any silly mistakes, that should generate a bunch of 11 character long, pseudo random strings that start with the letter "R".

I’m nowhere near a PC atm, so I can’t test this out, but the idea is:
Create a bunch of pseudo random 10 character strings (instead of 11 characters). And then output "R" plus each random string.

In the above, each 10 character string generated from /dev/urandom is redirected as input to a while loop, which uses the read command to read each line it receives into a variable called $line.
Inside the loop, we simply echo the character "R" plus the 10 character string in $line.
Yielding a bunch of 11 letter random strings beginning with the letter "R".

Again, at least it should….. I think!!
 
Another approach:
Code:
[flip@flop ~] $ cat /dev/urandom | LC_ALL=C tr -dc 'A-Z' | fold -w 11 | head -n 50 | sed 's/^./R/'
RKFXRFKMCRA
RPJFJTVKJBO
RPYXZHNYTFL
RINELEBPBSB
REJYTFCBLOF
RXCIQAMHPQL
RWUWFDYEJDP
RGUBVQJUGMJ
RACHJCPULLW
RWEQBUOVDDC
RLQSYVIMNRY
<snip>
 
Thank you! These are both very wise approaches.

@KGIII, not homework :) just someone not very skilled and willing to learn from others.
 

Members online


Top