LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Need Code to Randomly select Star Trek Series and Episode Number. (https://www.linuxquestions.org/questions/linux-general-1/need-code-to-randomly-select-star-trek-series-and-episode-number-4175610905/)

linustalman 07-30-2017 04:55 AM

Need Code to Randomly select Star Trek Series and Episode Number.
 
Hi.

Can someone make a command/script that can randomly generate one of these Star Trek series acronyms (tos, tng, ds9, voy, ent) and an episode from a corresponding number range?

Example output: voy25
That being the 25th episode overall in the Voyager series.

So if tos is selected then output a number from 1-79 (there were 79 episodes in The Original Series)

For tng 1-178. (The Next Generation had the most episodes at 178)

For ds9 1-176.

For voy 1-172.

For ent 1-98.

Thanks.

michaelk 07-30-2017 05:26 AM

There are several ways to generate random permutations depending on how random you want to get. One easy way is using shuf.

series=$(shuf -n 1 -e tos tng ds9 voy ent)

With a case or if statements you can generate an episode number based upon the series.
episode=$(shuf -i 1-178 -n 1)

output=$series$episode

Habitual 07-30-2017 05:28 AM

http://www.diveintopython.net/file_h...for_loops.html

GazL 07-30-2017 05:40 AM

removed. beaten to it

linustalman 07-30-2017 05:45 AM

Quote:

Originally Posted by GazL (Post 5741671)
I'm surprised someone who has been here since 2010 and has over 1600 posts never learned the basics of shell scripting. This is a completely trivial problem that took me less than 30 seconds (and that was just the time to type it). Where are you stuck?

hint: for loop using brace expansion piped into 'shuf -n1'

I'm not a coder.

GazL 07-30-2017 05:51 AM

Quote:

Originally Posted by linustalman (Post 5741672)
I'm not a coder.

It's an incredibly useful tool to have in your toolbox, even if you only spend a couple of hours to learn the very basics.

michaelk 07-30-2017 07:26 AM

Just because I am a Star Trek fan...

Code:

#!/bin/bash

series=("tos" "tng" "ds9" "voy" "ent")
episodes=("1-79" "1-178" "1-176" "1-172" "1-98")

num=$(shuf -i 0-4 -n 1)
num1=$(shuf -i ${episodes[$num]} -n 1)
output=${series[$num]}$num1

echo $output


linustalman 07-30-2017 01:39 PM

Quote:

Originally Posted by michaelk (Post 5741693)
Just because I am a Star Trek fan...

Code:

#!/bin/bash

series=("tos" "tng" "ds9" "voy" "ent")
episodes=("1-79" "1-178" "1-176" "1-172" "1-98")

num=$(shuf -i 0-4 -n 1)
num1=$(shuf -i ${episodes[$num]} -n 1)
output=${series[$num]}$num1

echo $output


Hi Michael. That's perfect! Thank you. :hattip:

linustalman 07-30-2017 02:12 PM

Feel free to vote on the poll I just added. Cheers guys.

khouji 07-31-2017 12:38 AM

Quote:

Originally Posted by michaelk (Post 5741693)
Just because I am a Star Trek fan...

Code:

#!/bin/bash

series=("tos" "tng" "ds9" "voy" "ent")
episodes=("1-79" "1-178" "1-176" "1-172" "1-98")

num=$(shuf -i 0-4 -n 1)
num1=$(shuf -i ${episodes[$num]} -n 1)
output=${series[$num]}$num1

echo $output


Fellow Jedi can you help me also with some codes?

michaelk 07-31-2017 04:47 AM

khouji,
If this is a serious question please create a new thread.

dugan 07-31-2017 01:31 PM

Quote:

Originally Posted by Habitual (Post 5741669)

I raise you:

https://docs.python.org/3/library/itertools.html (permutations and combinations)
https://docs.python.org/3/library/random.html (note "shuffle" method)

KenJackson 08-31-2017 08:58 PM

Here we have a poll about Star Trek and no one said one thing about Star Trek in 12 posts. Amazing.

I absolutely LOVED The Next Generation when it played originally, but every time I've watched a rerun I've been disgusted by the political correctness. Maybe I was just naive the first time around.

jefro 09-01-2017 02:28 PM

The TOS is the only one to watch. It always had two parts to the shows. One was tech and other was some sort of literary conflict. They didn't go overboard on PC. It was the only tech show at the time.

Zyblin 09-04-2017 04:05 PM

TOS of course. TNG very, very close second. But I also think they did a pretty good job on DS9, Voyager and Enterprise. I like the whole of it. When you add all of these together and see the larger picture of the Federation, the universe its in, etc. its great.

Individually though TOS is the one. It all started there.


All times are GMT -5. The time now is 09:29 PM.