It is one of the perks of being a member of the archaic hereditary UK monarchy that you have armed guards watching over you at all hours.

But that apparently almost backfired for the Queen when she decided to take a late-night walk around the palace grounds and came close to being shot.

From time to time her majesty is said to struggle to sleep. During a spell of insomnia, the 90-year-old will occasionally put on her coat and go for a short stroll, an ex-guardsman told The Times.

Late one night, he said he was patrolling inside the perimeter of the Palace walls when he spotted a figure in the darkness.

Thinking that he had caught a potentially dangerous intruder, sneaking into the Royal residence at 3am, he shouted: “Who’s that?”

But to his surprise, it was actually the Queen.

“Bloody hell, Your Majesty, I nearly shot you,” he is reported to have said, on impulse.

Queen's Guard loses his temper with tourist

Perhaps realising this wasn't the tone to be used when addressing the Queen, he told The Times he expected a telling off. But the Queen reportedly took responsibility for putting him in such an awkward position.

“That’s quite all right,” she replied. “Next time I’ll ring through beforehand so you don’t have to shoot me.”

It is believed the encounter happened at the Queen’s normal weekday residence, Buckingham Palace.

In 2013, Prince Andrew was also mistaken for an intruder and confronted by armed police as he walked in the vast gardens.

Police said no weapons were drawn but issued an apology to the prince, despite the palace having been subject to several real security breaches in recent years.

In August, a 22-year-old man was arrested just after 4am after climbing over a security fence. He was spotted by officers monitoring CCTV.

In May, Denis Hennessy, 41, climbed over barbed wire and roamed the palace grounds for about 10 minutes while The Queen, Prince Philip, and Prince Andrew were in residence.

When he was found by officers he repeatedly asked them: “Is Ma’am in?”

60 facts about the Queen you didn't know Show all 7 1 /7 60 facts about the Queen you didn't know 60 facts about the Queen you didn't know 4677429.jpg Since 1952 the Queen has given royal assent to more than 3,500 Acts of Parliament. AP 60 facts about the Queen you didn't know 5211094.jpg A royal footman who poured whisky into the corgis' water as a party trick was rewarded with a salary cut and a demotion. PA 60 facts about the Queen you didn't know 1805798.jpg When she crossed a corgi with a dachshund to create the dorgi the Queen perhaps unwittingly began the craze for designer cross-breeds. PA 60 facts about the Queen you didn't know 4883272.jpg The Royal Train comes with chefs, lace-trimmed pillows, and a no bumpy track rule during the Queen's 7:30am bath. AFP/Getty Images 60 facts about the Queen you didn't know 5173169.jpg The top video on Her Majesty's official YouTube Channel is a clip of Prince William and Kate leaving Buckingham Palace in an Aston Martin. Getty 60 facts about the Queen you didn't know 5180680.jpg Her Majesty has endured 36 Royal Variety Performances. Getty 60 facts about the Queen you didn't know 5337507.jpg On a state visit to Australia in 1954, during an argument with Prince Philip, the Queen was filmed "hurling shoes, threats and sporting equipment, and venting the sort of regal fury that, in another age, would have cost someone their head", according to writer Robert Hardman. "I'm sorry for that little interlude," she later said, "but, as you know, it happens in every marriage." AFP

Before that, in 2013, a man scaled the perimeter fence and was found at around 10pm actually inside the building, in one of the State Rooms. None of the royals were home at the time.

The news of the near-shooting comes at a time when the public is particularly tuned into the Queen’s wellbeing. The elderly monarch was not seen for a fortnight over the Christmas period due to poor health.