DeAndre Daniels had 15 points for UConn (9-1), which was 6 of 10 from behind the arc in the first half and 0 for 12 in the second.
Randle's jump shot with 3 1/2 minutes left gave the Cardinal a 52-49 lead. The Huskies pulled within a point when Daniels tipped in a missed 3-point attempt by Niels Giffey.
But Shabazz Napier missed three shots in the final 40 seconds with a chance to put the Huskies ahead. He gave up an opportunity on UConn's final possession, passing the ball to Omar Calhoun, whose 3-pointer from the right wing bounced off the rim.
"I felt like Omar was the most open guy at the time," Napier said. "I felt like it was going to go in, and I bet he did too."
Napier, who finished with 12 points and eight assists, missed two 3-pointers on a key possession with the Huskies trailing 52-51. Stanford got the ball back with 28 seconds left after a scrum underneath the basket.
Napier fouled Huestis on the inbounds play and he hit one of two free throws.
Napier, who beat Florida with a jumper at the buzzer earlier this month, missed another shot with 8 seconds left, forcing UConn to foul again.
He got the ball back on a rebound after Huestis missed another free throw. He dribbled down the court, but instead of trying a contested shot, he passed to Calhoun.
UConn led by 10 points at halftime and pushed the lead to 43-30, before consecutive 3-pointers by Anthony Brown and Randle. That started a 14-0 run by the Cardinal, who used a stifling zone defense to hold the Huskies without a field goal for more than 6 minutes. The Huskies were just 5 of 31 from the field in the second half.
"It was just a matter of us being aggressive on the defensive end," Randle said. "Just being active, getting our hands moving, getting our feet moving and getting our feet moving and just hustling."
A driving basket and free throw by Randle gave Stanford its first lead of the second half at 44-43.
"Our guys really stepped up on the defensive end," Dawkins said.
Napier's jumper from the lane put UConn back on top 45-44 with just over 10 minutes left and the teams went back and forth.
The Huskies closed the half on a 14-3 run that ended when Napier stole the ball from Randle and found Lasan Kromah ahead of the field for an easy layup just before buzzer.
UConn came in shooting better than 46 percent from beyond the arc, with five players having made 10 or more 3-pointers.
The loss snapped a 54-game home winning streak against nonconference opponents that dated to 2007.
Connecticut was coming off its annual hiatus for final exams. The Huskies had not played in 12 days, since a 95-68 win over Maine, a game in which UConn hit 14 3-pointers.
Stanford ended a 13-day break last Saturday with a 27-point win over UC Davis.
UConn plays at Washington, another Pac-12 team, on Saturday. The Huskies had dominated that conference, coming into Wednesday they were 17-2 and had won 13 straight since falling to UCLA in the 1995 NCAA West Regional championship.
Stanford's two losses came early in the season at home to BYU and to Pittsburgh in the Legends Classic on Nov. 26 in Brooklyn. The Cardinal return to the Barclays Center to face Michigan on Saturday.