Use the following ActiveRecord query to find a random record fulfilling a condition:

Topic.order('RANDOM()').where(exam_id: 1).first