replace math.random with torch.random