2011-03-09 25 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