-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.sh
49 lines (40 loc) · 1.7 KB
/
random.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# **************************************************************************** #
# #
# ::: :::::::: #
# random.sh :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tvandorm <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/12/19 11:58:22 by tvandorm #+# #+# #
# Updated: 2024/01/16 15:26:32 by tvandorm ### ########.fr #
# #
# **************************************************************************** #
#!/bin/bash
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <n> <min> <max>"
exit 1
fi
n=$1
min=$2
max=$3
if [ $n -le 0 ]; then
echo "Please specify a positive integer for n."
exit 1
fi
if ! [[ "$n" =~ ^[0-9]+$ ]]; then
echo "Please specify a positive integer for n."
exit 1
fi
if [ $min -ge $max ]; then
echo "min should be less than max."
exit 1
fi
# Check if the range is sufficient
if [ $n -gt $((max - min + 1)) ]; then
echo "The available range of random numbers is insufficient."
exit 1
fi
# Generate n random unique numbers and concatenate them into a single line
numbers=$(awk -v n=$n -v min=$min -v max=$max -v seed="$(date +%s%N)" 'BEGIN{srand(); while (c < n) {x = int(min + rand() * (max - min + 1)); if (!(x in arr)) {arr[x]; printf "%s ", x; c++}}}')
# Print the concatenated numbers
echo $numbers