【发布时间】:2013-04-16 03:27:21
【问题描述】:
我正在尝试设置我的活动,以便我可以从我的 onCreate 方法之外的方法生成一组随机数。这是我的活动的设置方式...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.optionOne();
this.optionTwo();
this.optionThree();
}
public void optionOne() {
// generate a random number here
int random = Math.random();
// generate more random numbers and do more stuff here
}
问题是,我在 onCreate 方法之外生成的任何随机数都被认为是“静态的”,并且数字始终为 0。如果我在 onCreate 方法中生成数字,它当然可以正常工作。我该如何解决这个问题?
【问题讨论】:
-
Math.random() 返回 0.0 到 1.0,如果你称它为 'int',它会舍弃小数,将其保留为 0。
-
@wtsang02 - 实际上我在做 Math.random(max_value),我只是删除了多余的数字以简化示例。虽然,您的其他答案解决了我的问题!谢谢你:)
标签: android methods random static