2

I want to generate the random numbers and then use it as the "index" to get the data from other array list.

Given a number 5, and then generate 5 numbers in the sequence of [1..5], and then shuffle them into the random order.

Here are two examples output after shuffling the numbers:

1, 3, 5, 2, 4
2, 5, 1, 4, 3

Gien a number 100, then generate 100 numbers in [1...100], then shuffle them using the same method.

Now define a shuffle function in bash:

shuffle_numbers() {
    local range="$1"
    
    # Use method from "Mark Setchell"
    # numbers=( $seq 100 | shuf )
    
    # The value of numbers should be:
    # 1, 3, 5, 2, 4
    # or
    # 2, 5, 1, 4, 3
    # or
    # ..other random orders if the $range is 5.
    # or
    # ... 100 random orders if the $range is 100...
    
    sprintf "%s" "$numbers"
}

How to implement the shuffle_numbers in bash?

stackbiz
  • 1,136
  • 1
  • 5
  • 22
  • Does this answer your question? [How to generate random number in Bash?](https://stackoverflow.com/questions/1194882/how-to-generate-random-number-in-bash) – Paolo Aug 28 '23 at 09:56
  • That link can't solve this question as the output does not match. I want to get a list of all "random" but not "duplicated" numbers as the return result instead of just return only 1 random number in that range. – stackbiz Aug 28 '23 at 09:59
  • I do not understand. `generate the random numbers and then use it as the "index"` Do you want to generate one random number and index another array with it? Or you want to generate a list of random numbers and generate indexes or another array? __or__ do you want to shuffle an array? Is this XY question? `That link can't solve` Does https://stackoverflow.com/a/38865224/9072753 "solve" it with `seq` command? – KamilCuk Aug 28 '23 at 10:06
  • 1
    Generate the numbers and shuffle them into an array. Pop one element on each call. `numbers=( $seq 100 | shuf) )` – Mark Setchell Aug 28 '23 at 10:08
  • I think "shuffle" is the correct word for this question. I tried "numbers=( $seq 100 | shuf)" but got "bash: syntax error near unexpected token `|'". – stackbiz Aug 28 '23 at 10:17
  • 1
    @EdMorton Yes, I agree. I think that is what the OP wants. – Mark Setchell Aug 28 '23 at 10:17
  • 1
    Sorry, I meant `a=( $(seq 10 | shuf ))` – Mark Setchell Aug 28 '23 at 10:20
  • 1
    I have updated the question, please check again, thank you all. – stackbiz Aug 28 '23 at 10:29
  • FYI there's no `sprintf` in bash. The equivalent to `var=$( sprintf '%s' "$numbers" )`, if it existed, is `printf -v var '%s' "$numbers"` – Ed Morton Aug 28 '23 at 11:13
  • [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – glenn jackman Aug 28 '23 at 12:51

1 Answers1

2

Here is what I was suggesting in the comments...

Create a sequence of 10 numbers, 1..10 and shuffle them into an array a:

a=( $(seq 10 | shuf ))

Examine array contents:

echo "${a[*]}"
1 2 9 5 8 4 7 10 6 3

Pop last element, examine it and remove from array:

r="${a[-1]}"
echo "$r"
3    
unset 'a[-1]'        # remove last element
echo "${a[*]}"       # check what's left
1 2 9 5 8 4 7 10 6

I think unset requires bash v4.3 or so, as that aspect won't work on the ancient bash v3.2 that Apple ships with macOS.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432