Ahead of the series finale for the hit television show The Big Bang Theory, Canadian musician Ed Robertson shares how he came up with the catchy theme song while he was barenaked in the shower.

The legendary frontman for the Canadian alternative rock band Barenaked Ladies said it was a twist of fate that brought the group and the long-running sitcom together.

Twelve years ago, Robertson said he and the band had no idea famed television writers Chuck Lorre and Bill Prady were in the audience at one of their L.A. concerts.

Coincidentally during that show, the self-proclaimed “science nerd” says he performed a freestyle rap about the Earth’s origins, inspired by Simon Singh’s book ‘Big Bang: The most important scientific discovery of all time and why you need to know about it.’

“I was just making up a silly song about the astronomer Hubble and standing on the shoulders of giants and cosmological theory and Billy Prady and Chuck Lorre were at the show and that’s what made them think that they had to get me to write their theme song for the show,” he told CTVNews.ca during a telephone interview on Wednesday.

“It all came to me in a flash”

Robertson admitted he was initially hesitant to accept their offer because he had written music for films and television shows in the past, but they were never picked up. And to top it off, he was feeling “burnt out” from touring. He credits his wife with persuading him to embrace the opportunity.

“I told her ‘I’m supposed to do this conference call for a television theme song, but I haven’t written it so I think I’m just going to get on the call and let them know that they need to find somebody else’ and my wife said ‘Well, who’s it for?’ and I said ‘Some guy named Chuck Lorre’ and she said ‘Chuck Lorre? You should probably Google him,’” he said with a laugh.

Minutes later, Robertson said the lyrics for the song just came to him while he was in the shower.

“I had to call my wife in. It all came to me in a flash. I had to call her and say ‘Can you write this down before I forget it?’ [I had] shampoo in my face,” he recalled.

Afterwards, he recorded the song on his computer and sent it to Lorre and Prady.

“They loved it,” he said.

Since then, the minute-and-45-second tune that covers everything from the Neanderthals to Einstein has become a staple in the Barenaked Ladies’ set list, even landing a spot in the band’s greatest hits album “Hits from Yesterday & the Day Before” in 2011.

“I would say when the show aired, I never anticipated that we’d be playing this song live,” Robertson said. “If we play four songs it’s going to be “One Week,” “If I Had a Million Dollars,” “Pinch Me,” and “The Big Bang Theory,” the rest can rotate in and out.”

Sitcom success

As for whether the band’s proceeds from the theme song have finally allowed him to buy a fur coat, a monkey, and Kraft Dinner -- as per their hugely successful song “If I Had a Million Dollars” -- Robertson said he would have bought those things if it wasn’t for his wife’s sensibilities.

“It’s been very good to me,” he said.

The television show has also been very good to the Barenaked Ladies in the years since its premiere, Robertson said.

“They had us down for their 100th episode taping and we performed for their live audience. It’s just been a really good relationship,” he shared.

As for how he feels about the show’s final episode airing Thursday night after 12 seasons, Robertson described the emotion as bittersweet.

“In some ways it’s sad that it’s ending, but it’s also really great that it’s going out on top where nobody’s sick of it and it’s a perfect time to wrap it up,” he said.

For fans who will miss the familiar fast-paced theme song at the start of every episode of The Big Bang Theory, Robertson said the Barenaked Ladies have several tours planned where the tune will inevitably make an appearance.

“We’ve got a 50-show tour of America starting at the end of this month. U.K and Canadian tours in the works. A new record in the works. It never stops,” he said.

The final episode of The Big Bang Theory airs Thursday, May 16 at 8 p.m. ET/PT on CTV, CTV.ca and the CTV app.