This question is about generating random numbers between a range, which is fine, but it doesn't fit my case.
I'll explain in SQL terms because it seems to me that's easier to understand, though the question is about
bash. My idea is with the results of the
bash code build a SQL script.
I have two MySQL tables, one of people and one of places. Each record has an unique integer id which goes from 1 to 139 (places) and 1 to 1519 (people). They are linked to each other by a foreign key, meaning: a place can have many people, but a person can have only one place.
# 1-139 # 1-1519 place1 → person1 → person2 → person3 ... and so on
The data I have right now is that in one place all the people are linked, and the rest of places without any.
The places are 139 and the people are 1519, so I have one place with 1519 people.
My goal is to distribute the people randomly to the places, and that each place has at least one person.
My code so far is this:
$ c=1519 $ while [[ $c -ne 0 ]]; do x=$((shuf -i 1-139 -n 1)) [[ $x -gt 139 ]] && continue echo $x (( c-- )) done
This code generates 1519 random numbers between 1-139, so now I can have each person linked to a random place.
My questions are:
- Is there a more efficient way to accomplish this?
- How can I control that each place has at least one person?
I prefer to do this in
bash, but I'm open to other solutions not involving it.