Background Molecularly targeted agents have been reported to have anti-tumour activity for patients whose tumours harbour the matching molecular alteration. These results have led to increased off-label use of molecularly targeted agents on the basis of identified molecular alterations. We assessed the efficacy of several molecularly targeted agents marketed in France, which were chosen on the basis of tumour molecular profiling but used outside their indications, in patients with advanced cancer for whom standard-of-care therapy had failed. Methods The open-label, randomised, controlled phase 2 SHIVA trial was done at eight French academic centres. We included adult patients with any kind of metastatic solid tumour refractory to standard of care, provided they had an Eastern Cooperative Oncology Group performance status of 0 or 1, disease that was accessible for a biopsy or resection of a metastatic site, and at least one measurable lesion. The molecular profile of each patient's tumour was established with a mandatory biopsy of a metastatic tumour and large-scale genomic testing. We only included patients for whom a molecular alteration was identified within one of three molecular pathways (hormone receptor, PI3K/AKT/mTOR, RAF/MEK), which could be matched to one of ten regimens including 11 available molecularly targeted agents (erlotinib, lapatinib plus trastuzumab, sorafenib, imatinib, dasatinib, vemurafenib, everolimus, abiraterone, letrozole, tamoxifen). We randomly assigned these patients (1:1) to receive a matched molecularly targeted agent (experimental group) or treatment at physician's choice (control group) by central block randomisation (blocks of size six). Randomisation was done centrally with a web-based response system and was stratified according to the Royal Marsden Hospital prognostic score (0 or 1 vs 2 or 3) and the altered molecular pathway. Clinicians and patients were not masked to treatment allocation. Treatments in both groups were given in accordance with the approved product information and standard practice protocols at each institution and were continued until evidence of disease progression. The primary endpoint was progression-free survival in the intention-to-treat population, which was not assessed by independent central review. We assessed safety in any patients who received at least one dose of their assigned treatment. This trial is registered with ClinicalTrials.gov, number NCT01771458. Findings Between Oct 4, 2012, and July 11, 2014, we screened 741 patients with any tumour type. 293 (40%) patients had at least one molecular alteration matching one of the 10 available regimens. At the time of data cutoff, Jan 20, 2015, 195 (26%) patients had been randomly assigned, with 99 in the experimental group and 96 in the control group. All patients in the experimental group started treatment, as did 92 in the control group. Two patients in the control group received a molecularly targeted agent: both were included in their assigned group for efficacy analyses, the patient who received an agent that was allowed in the experimental group was included in the experimental group for the purposes of safety analyses, while the other patient, who received a molecularly targeted agent and chemotherapy, was kept in the control group for safety analyses. Median follow-up was 11·3 months (IQR 5·8–11·6) in the experimental group and 11·3 months (8·1–11·6) in the control group at the time of the primary analysis of progression-free survival. Median progression-free survival was 2·3 months (95% CI 1·7–3·8) in the experimental group versus 2·0 months (1·8–2·1) in the control group (hazard ratio 0·88, 95% CI 0·65–1·19, p=0·41). In the safety population, 43 (43%) of 100 patients treated with a molecularly targeted agent and 32 (35%) of 91 patients treated with cytotoxic chemotherapy had grade 3–4 adverse events (p=0·30). Interpretation The use of molecularly targeted agents outside their indications does not improve progression-free survival compared with treatment at physician's choice in heavily pretreated patients with cancer. Off-label use of molecularly targeted agents should be discouraged, but enrolment in clinical trials should be encouraged to assess predictive biomarkers of efficacy. Funding Institut Curie.