Member Avatar for OldDeveloper01

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();
}

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)]);

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.