0

I have this block of code below which is to control questions which are to be displayed from the array.
I am having problem with the the 3rd line of code, an i am not entirely sure what the problem is.

rnd1, rnd2 are both double. Eclipse is telling me that rnd2 should be an int. however i was advised that the rnd's should be double for the ceiling function to work.
ques is a text field. questions is the array.

rnd1 = Math.ceil(Math.random()*3);
rnd2 = Math.ceil(Math.random()*questions.length)-1;
ques.setText(questions[rnd2]);

-------------

Im basing this on action script which i used for a quiz application.
It is used to pick out the question from the question array randomly.

rnd1=Math.ceil(Math.random()*3);
rnd2=Math.ceil(Math.random()*questions.length)-1;
q.text=questions[rnd2];
if(questions[rnd2]=="x")
{
    change_question();
}
3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by m4ster_r0shi
0

What if you cast rnd2 to an int?

ques.setText(questions[(int) rnd2]);

Note that you don't really need Math.ceil here. You could just do...

ques.setText(questions[(int) (Math.random() * questions.length)]);

Edited by m4ster_r0shi

0

The Random class has a nextInt() method you could use to generate an index into the array.

0

Ah, right, I forgot about that one. And here you can see why you should prefer Random.nextInt(n) to (int) (Math.random() * n).

Edited by m4ster_r0shi

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.