NEW YORK (Reuters) - A federal judge ruled on Friday that police had a constitutional right to randomly search passengers' bags on the New York City subway to deter terrorist attacks.

U.S. District Judge Richard Berman ruled the searches were an effective and appropriate means to fight terrorism, and constituted only a "minimal intrusion" of privacy.

"The risk to public safety of a terrorist bombing of New York City's subway system is substantial and real," Berman wrote in his opinion.

"The need for implementing counter-terrorism measures is indisputable, pressing, ongoing and evolving."

Random bag searches began on July 22 after a second set of bomb attacks on London's transit system.

In a statement, Mayor Michael Bloomberg praised the ruling, calling bag searches a "reasonable precaution" that police would continue to take.

The New York Civil Liberties Union (NYCLU), which had sued to stop the searches, plans to appeal, Executive Director Donna Lieberman said in a statement. She said the "unprecedented" bag search program violated a basic freedom.

More than 4 million people a day ride the 101-year-old subway system, the nation's largest.

The NYCLU had sued the city and Police Commissioner Ray Kelly in early August, calling the policy of searching thousands of passengers a day without any suspicion of wrongdoing unconstitutional.

The Fourth Amendment to the U.S. Constitution prohibits searches without probable cause.

Police had argued random searches were a crucial deterrent to a possible attack.

The frequency of searches increased in October after Bloomberg said the FBI alerted him to a specific threat to the subway system. Searches were later reduced after the federal warning passed without incident.