Setting range for array
I need to set the range of numbers for my array where by I need to give a set maximum and minimum number to my array. This is what I thought up off but it isn't very efficient cause it will take too long for my program to execute
do{
for (int i=0; i<array.length-2; i++)
{
array[ i ] = ( 1 + (int)(Math.random() * arraySize * arraySize + (int)Math.random()));
for(int d = 1; d >< array.length; d++){
if(array[d] > max){
max = array[d];
}//end if
else
if(array[d] < min){
min = array[d];
}//end if
}//end for
}//end for loop
}//end do while
while (max!=2421 && min != 1);
can anyone help me by suggesting a more efficient method?
> can anyone help me by suggesting a more efficient
> method?
Not quite sure if I understand correctly.
Do you want to generate (pseudo) random numbers betwee n two specific numbers? If so, you'd better use the java.util.Random class; it has more functionality than the Math.random() method, and does a better job generating (pseudo) random numbers.
Here's a small example:
static java.util.Random rand = new java.util.Random();
/**
* @param min
* @param max
* @return Returns a (pseudo) random number between
* 'min' (inclusive) and 'max' (exclusive).
*/
public int getRandomBetween(int min, int max) {
if(max-min < 0)
throw new IllegalArgumentException("max-min < 0");
return rand.nextInt(max-min)+min;
}
aslo if you are going to continue to post code here, use code tags
that is the word "code" surrounded by square brackets at the start and "/code" is square brackets at the end.
Failure to do this is what screwed up your other post when you tride to write A sub i. If you put brackets around the i and you are NOT inside a code tag, the i in brackets italicizes the rest of the message.
you can put spaces in like this A[ i ] and should have no problem but if you just write A the i subscript never shows. And the rest of your message is curiously in italics, like this.
Use code tags also to switch to a fixed size font if you want to do typewriter artwork.
Last but not least, notice the preview button next to the post. Many simple errors are fixed by previewing first.
Enjoy!
Similar to setting range for array, how can we generate a random number and have it confine to a rangeI have so far discoverd that doing 1+(int)Math.random()*10 generates a range from 1 to 11. but i am trying to generate a random number from 0-3, Help please
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html#nextInt(int)
> but i am trying to generate a random number from 0-3
Do you mean 0,1 or 2?
[0-3)
nextInt(3)
Or
Do you mean, 0, 1, 2 or 3?
[0-3]
nextInt(4)