2011-03-09 23 views

回答

1
int result = (WhateverRandomNumbersComeFromInObjectiveC() >= 0.5) ? 1 : -1; 
+0

大聲笑很好:) – iluxa 2011-03-09 20:14:52

+2

難道你不喜歡那些漫長的Objective-C方法名嗎? – Ferruccio 2011-03-09 20:16:51

2
int random = arc4rand()%2 ? -1 : 1; 
1
#include <stdlib.h> 
(arc4random() % 2) * 2 - 1 
  • arc4random()給出的隨機整數
  • (arc4random()%2)使得它是0或1
  • (arc4random()%2)* 2使其爲0或2
  • (arc4random()%2)* 2 - 1給出-1或1