1

I would like to create four strings, each with a random length, but their total length should be 10. So possible length combinations could be:

3    3    3    1

or

4    0    2    2

Which would then (respectively) result in strings like this:

111    222    333    4

or

1111    33    44

How could I do this?

5
  • 2
    4+0+2+2=8, or am I missing something? Commented Oct 29, 2015 at 12:25
  • This seems possible. Edit your question to tell us : Does randomness cross lines? (are you trying to write a suduko generator?) Most importantly, edit your question to show what have you tried ;-) Good luck Commented Oct 29, 2015 at 12:30
  • @Zloj Nope, I want the lenghts to be generated randomly. I'm sorry, maybe I wasn't clear enough, I've added an example. Commented Oct 29, 2015 at 12:31
  • @shellter Thanks for your response. I've added an example, maybe this makes things clearer. I've managed to create random-length strings with the method described here (gist.github.com/earthgecko/3089509), but not to create four random-length strings that together have a length of precisely 16. Commented Oct 29, 2015 at 12:33
  • you're heading quickly to an close vote for 'unclear what you're asking'. length of 10, or 16? Also, don't expect people to go to a remote site and have to search around and decide what is relevant. If there is a key point on that site, copy into your question and include a link for the curious. Please ;-) Good luck. Commented Oct 29, 2015 at 12:38

3 Answers 3

3

$RANDOM will give you a random integer in range 0..32767. Using some arithmetic expansion you can do:

remaining=10

for i in {1..3}; do
    next=$((RANDOM % remaining)) # get a number in range 0..$remaining
    echo -n "$next "                 
    ((remaining -= next))
done
echo $remaining

Update: to repeat the number N times, you can use a function like this:

repeat() {
    for ((i=0; i<$1; i++)); do
        echo -n $1
    done
    echo                                         
}

repeat 3
333
Sign up to request clarification or add additional context in comments.

1 Comment

All three answers were great, so I've upvoted all three. However, I chose this answer as the preferred one, since it's the most extensive one. Thanks everybody!
3

Here is an algorithm:

Make first 3 strings with random length, which is not greater than sum of lenght (each time substract it). And rest of length - it's your last string.

Consider this:

sumlen=10
for i in {1..3}
do
   strlen=$(($RANDOM % $sumlen)); sumlen=$(($sumlen-$strlen)); echo $strlen
done
echo $sumlen

This will output your lengths, now you can create strings, suppose you know how

Comments

2

alternative awk solution

 awk 'function r(n) {return int(n*rand())} 
      BEGIN{srand(); s=10; 
            for(i=1;i<=3;i++) {a=r(s); s-=a; print a} 
            print s}'

3
5
1
1

srand() to set a randomized seed, otherwise will generate the same random numbers each time.

Here you can combine the next task of generating the strings into the same awk script

$ awk 'function r(n) {return int(n*rand())}; 
       function rep(n,t) {c="";for(i=1;i<=n;i++) c=c t; return c}
       BEGIN{srand(); s=10; 
             for(j=1;j<=3;j++) {a=r(s); s-=a; printf("%s ", rep(a,j))}
             printf("%s\n", rep(s,j))}'

generated

1111 2 3 4444

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.