Robert S. McNamara predicted here tonight that white-minority rule in South Africa will be overthrown in a racial explosion within 20 years and advised the United States to make clear now that it will not come to the aid of the whites when this happens.

McNamara said the United States and other Western countries should hasten the stockpiling of chromium, vanadium, manganese and platinum in anticipation of an export cutoff by South Africa to retaliate for such a U.S. declaration.

He said many black Africans and mixed-race Coloreds to whom he has spoken on his current visit believe the United States is supporting the South African government in its discriminatory policies and would aid it in a racial confrontation with the black majority.

Emphasizing that he did not believe that to be the case, McNamara stressed that the United States "must make it clear we will not do so, even if the withholding of U.S. support carries with it the risk that South Africa will fall within the Soviet sphere of influence."

The former secretary of defense and president of the World Bank was delivering the Chancellor's Lecture at the University of the Witwatersrand, South Africa's largest English-language university, which has a long liberal tradition. He amplified some of his points, notably his predicted time frame for the overthrow of white rule, in an interview.

McNamara delivered his lecture to a predominantly white audience. There were a few black students recently admitted to the university under a relaxation of the government's segregation system known as apartheid. He dismissed constitutional-reform plans proposed by the government as inadequate, saying they are far too slow and fail to confront the core issue of black political participation.

These reforms propose extending parliamentary representation to the Coloreds and Asians in segregated chambers, but they would continue to exclude the voteless black majority.

Blacks were reluctantly concluding that fundamental change could come about only through revolutionary violence, he said, citing an estimate that 8,000 young blacks had already left for military training abroad.

McNamara said the blacks still lacked the means to mount any sustained military challenge to white authority, but this would change. The front-line African states would gain in strength and resources, and would probably get Soviet, Cuban and East German support to help protect themselves against retaliation by South Africa.

"The ultimate aim of such a conflict, unless it were to be influenced by the introduction of Western military power, hardly seems in doubt," McNamara said.

McNamara said African leaders with whom he had talked outside South Africa believe the confrontation will come in five to ten years. When questioned about this in the interview, McNamara said his own estimate is 10 to 20 years.

He foresaw repression within South Africa pushing the focus of the black struggle outside the country's borders, the Soviets intervening to support the front-line states as guerrilla havens, and the West finding itself unable to stand in strategic alliance with South Africa.

"To support South Africa would be regarded as supporting a political system profoundly insulting to the basic human rights of the majority of its population, and hence unacceptable to the peoples of the West," he said.

McNamara would not be drawn into whether he believes American business should continue to invest in South Africa or disinvest. He said he was still examining the issue.

He also said the International Monetary Fund should grant a $1.7 billion loan, requested by South Africa, because it was important for the IMF not to be influenced by political considerations.