Robert Buckland, 18, carried out a "premeditated experiment in violence" on Alison Kennedy and it was a miracle that she had survived, Judge Geoffrey Grigson said at the Old Bailey.
Miss Kennedy, 28, is still receiving treatment for the injuries she received on a Waterloo to Guildford train in March last year and has had major surgery since Buckland was convicted of attempted murder five months ago.
He showed no emotion as the judge ordered him to be detained without limit under the Mental Health Act, but moments before, he apologised through his counsel, Anthony Brigden, for ruining Miss Kennedy's life.
"No one seems to understand this extraordinary offence, least of all the defendant," Mr Brigden said. "There was no motive or logic in this random and bizarre attack."
Buckland, a homeless loner, had maintained that he was trying to snatch Miss Kennedy's handbag as she sat in a deserted carriage but Judge Grigson said it had never been an attempted robbery.
He added that such an offence would ordinarily demand a severe sentence and would justify life imprisonment but doctors were unanimous that Buckland suffered from a psychopathic personality disorder and was likely to respond to treatment.
Judge Grigson said that if the attack had happened after a recent law was passed giving courts the power to pass life sentences linked with hospital treatment, that is what he would have done.
Buckland presented a grave and continuing danger to the public, he added.
During the trial, the court heard that Buckland had fantasised about knifing a woman in the head while he was at school. When he spotted Miss Kennedy alone in the carriage on the way to visit her sister, he saw his opportunity.
He plunged a Bowie-style hunting knife deep into her skull from behind, intending to kill her, but when she turned and stood up, holding the knife in her head, he ran away and tried to jump from the train.
Miss Kennedy was left to stagger along the train for help with the five- inch blade embedded in her skull.
Detective Chief Inspector Dick Wainer, of the British Transport Police, who rang Miss Kennedy to tell her of the sentence, said she was very pleased. "She wants to get on rebuilding her life," he said. "She was remarkably well considering the trauma she has been through. She is recovering physically, but the mental scars will there for the rest of her life."
Miss Kennedy had worked in London as an arts festival co-ordinator after obtaining a degree in arts and design and completing a post-graduate course in sculpture.
Last year she returned to the workforce as an education officer for the Multiple Sclerosis Society in Northern Ireland and was described as "a dedicated and forward-planning young person".
Her family said they were relieved their ordeal was over and that they hoped that Buckland would spend the rest of his life in custody.
Miss Kennedy's mother, Helen, said: "We are relieved that the case has now been concluded and we hope that Buckland will spend the rest of his life, or least 20 years, locked up.
"The seriousness of his crime will never be forgotten by us and it has been a long wait by us for this."