Funny how things work out sometimes.

Back in 2001, Les Miles beat out 34-year-old Mike Gundy to become head coach at Oklahoma State — Gundy’s alma mater, where he had been a record-setting quarterback.

Disappointed but realistic, Gundy accepted Miles’ offer to become offensive coordinator. And four years later, when Miles departed for LSU, it took Oklahoma State only two days to elevate Gundy to the position he’d sought before.

“I’m not sure I was ready when I did get the job,” said Gundy, whose Cowboys will face Ole Miss on Friday in the Sugar Bowl. “I learned a lot of football from coach Miles. “I was just fortunate to follow up behind him. So that was probably for the best back then.”

It certainly turned out well for Oklahoma State.

Under Gundy, the Cowboys have had 10 straight winning seasons and are in a New Year’s bowl for the third time in five years.

Gundy’s 94-46 record has finally lifted Oklahoma State, a traditional in-state second fiddle to Oklahoma, to above .500 all-time.

To be sure, Oklahoma State still trails the Sooners in most regards.

Under Gundy, they’re only 2-9 against them — including a 58-23 beatdown this season, which denied the Cowboys a share of the Big 12 championship and propelled Oklahoma to the CFP semifinals.

Gundy has only one league title, and that one in 2011 was tempered by a 37-31 overtime loss to unranked Iowa State that cost OSU a shot against LSU and Miles in the BCS championship game.

This season, Oklahoma State won its first 10 games and rose to No. 6 in the CFP rankings.

But a 45-35 loss to Baylor, followed the Oklahoma game, knocked the Cowboys to No. 16 in the final CFP standings, although they did win the tiebreaker with Baylor and TCU to earn their first Sugar Bowl berth in 70 years.

And to Gundy, that’s not so bad.

“We’ve worked hard to establish a true football tradition,” he said. “We’re in a state where if there are five top prospects in Oklahoma, we know the Sooners are probably going to get four of them. So we’ve had to work a little harder and dig a little deeper to equalize things.”

Of course, it helped that during Gundy’s first season, mega-booster T. Boone Pickens donated $165 million to the school’s athletic department, the largest such contribution in NCAA history.

“Before that, we never thought we had a chance to be competitive in football on a regular basis,” said OSU Athletic Director Mike Holder. “That donation changed the whole paradigm. Hope is a powerful motivation.”

Keeping your coach in place helps, as well.

Going into next season, only four other coaches will have been at their current schools longer than Gundy at OSU (or Miles at LSU, for that matter).

Not that there haven’t opportunities for Gundry to go elsewhere.

His name is regularly mentioned when a major job opens, most prominently Tennessee in 2013.

And sure enough, when Miles was supposedly on the chopping block back in November, Gundy’s was listed among the contenders to be next in line.

“I look on that as a compliment,” Holder said. “You want your coaches to have the kind of success that has other schools looking longingly at them. When people are interested in your football coach, I’d classify that as a high-class problem.”

But Gundy sees another problem — coaches who have been at a school for a decade or more getting nudged out the door when they’re not meeting increased expectation levels.

“I was just dumbfounded by Les’ situation,” he said. “Everybody has an opinion, and mostly it’s ‘What have you done for me lately?’ Whenever a coach has been at a place for a while, people start thinking there’s got to be somebody out there better than our guy.”

Gundy hasn’t had to hear much of that. In part it’s because Oklahoma State’s previous football history prevents the lofty expectation level of an Oklahoma or an LSU.

Plus, Gundy is, in Holder’s words, a Cowboy for life.

Not only is he an OSU graduate, he spent 10 years there as an assistant before becoming the head coach.

In the special culture of Oklahoma State, having deep ties to the school means a lot. The baseball, wrestling and golf coaches also are alums, as is Holder.

That has also helped keep Gundy at Oklahoma State, although he’s said he’s due a rolling five-year contract extension in appreciation for that loyalty.

“I’d love to stay at Oklahoma State for the rest of my career,” Gundy said. “But I also need to know my administration is behind me 100 percent.

“If they want to make that commitment to me, then I want to make that commitment to them. There’s a certain side of it that’s always going to be a business.”

So stay tuned. As it always is in college football, things have a funny way of working out.