WASHINGTON — The Supreme Court on Wednesday gave federal judges wide latitude in imposing punishments in an unusual, two-part decision that struck down sentencing rules used to set prison time for hundreds of thousands of criminals since 1987.

In a 5-4 decision written by Justice John Paul Stevens, the court said that federal sentencing rules violate defendants' right to a jury trial by allowing judges — rather than juries — to decide on factors that can increase sentences beyond certain ranges.

But a separate 5-4 majority, led by Justice Stephen Breyer, said the rules designed by Congress to bring uniformity and fairness to punishments shouldn't be junked. Instead, they should be considered by judges on an advisory basis rather than as mandates.

The seemingly contradictory opinions created widespread uncertainty over the federal sentencing system and could lead thousands of convicts to flood courts across the nation with appeals of their sentences.

The disparate opinions came about because one justice, Ruth Bader Ginsburg, first sided with four colleagues who said the sentencing rules were unconstitutional because they forced judges to increase prison time based on certain factors — such as the amount of drugs involved in a crime — without putting such questions to jurors.

Then, without saying why, Ginsburg switched to Breyer's side to form a majority that said the remedy for the problem was to allow the rules to guide judges toward a sentence, rather than automatically require certain prison terms.

Breyer's majority said the decision should allow judges to hand down sentences tailored to specific defendants' conduct. He said that judges still should look to the guidelines and that sentences should be upheld on appeal as long as they are "reasonable."

But Justice Antonin Scalia, who was among those who said the rules were unconstitutional, said it is impossible to know how Breyer's plan will work. He predicted that it will "wreak havoc" on courts.

Assistant Attorney General Christopher Wray expressed disappointment in the decision, which could hamper federal prosecutions. Judges now have greater authority to reject prosecutors' grounds for tougher sentences and to find their own grounds for handing down lighter sentences than what the guidelines dictated.

"There are many things that are unclear" about the decision's impact, said Frank Bowman, an Indiana University law professor. "But one thing that appears clear is that judges will have more sentencing power than they have had in at least a century."