# nextint() Method !

• October 24th, 2010, 01:24 PM
M7MD
nextint() Method !
hi all :)

i need help in this exercise :-

develop main method that initializes a random object with the default constructor and then determines the elapsed time for the nextint() method to generate 123456789 :confused:

-------

• October 24th, 2010, 01:37 PM
copeg
Re: nextint() Method !
Define help...what do you have so far? If nothing, start here: The Java™ Tutorials
• October 24th, 2010, 01:45 PM
M7MD
Re: nextint() Method !
Quote:

Originally Posted by copeg
Define help...what do you have so far? If nothing, start here: The Java™ Tutorials

:-o

this is my basic idea

Code Java:

``` Random r = new Random();   for(int i=1;i<10;i++){ int Number = r.nextInt(10); System.out.println(Number);```

this code generate 9 random numbers , now i need to generate 123456789 using this method

anyone help me
• October 24th, 2010, 02:17 PM
M7MD
Re: nextint() Method !
ok i trying in this idea :-

Code Java:

```Random r = new Random(); for(int i=0;i<9;i++){ int[] array=new int[9]; int Number = r.nextInt(10); if((Number==1) || (Number==2) || (Number==3)||(Number==4)||(Number==5)||(Number==6)||(Number==7)||(Number==8)||(Number==9)) Number=array[i]; System.out.println(array[i]); }```

the output is :

0
0
0
0
0
0
0
0
0

Why !! why it not working
• October 24th, 2010, 02:25 PM
copeg
Re: nextint() Method !
When equating two values, the left operand is assigned the value of the right. Inspect your code carefully and you will see that the values within the array are never assigned anything (and thus you print their default value of 0)
• October 24th, 2010, 02:42 PM
Darryl.Burke
Re: nextint() Method !
• October 24th, 2010, 02:55 PM
M7MD
Re: nextint() Method !
???????????
• October 25th, 2010, 04:37 AM
Darryl.Burke
Re: nextint() Method !
Quote:

???????????
!!!!!!!!!!!
• October 25th, 2010, 09:39 AM
helloworld922
Re: nextint() Method !
Quote:

Originally Posted by M7MD
Code Java:

``` int Number = r.nextInt(10); if((Number==1) || (Number==2) || (Number==3)||(Number==4)||(Number==5)||(Number==6)||(Number==7)||(Number==8)||(Number==9))```

That condition is completely unnecessary. The definition of nextInt() is that it will return an integer in the range [0,10). Simply change your random-number generation statement to this and there's no need for an if statement.

Code Java:

```int number = r.nextInt(9) + 1; // no need for an if statement, number is guaranteed to be in the range [1,10] array[i] = number; // ...other code```
• October 25th, 2010, 09:40 AM
JavaPF
Re: nextint() Method !
I would do it like this:

Code Java:

```import java.util.Random;   public class Numbers {   /** * JavaProgrammingForums.com */ public static void main(String[] args) {   Random r = new Random();   int Number; int Count = 1;   while(Count != 10){   Number = r.nextInt(10)+1;   if(Number == Count){ System.out.print(Number + " "); Count++; } } } }```

Output:

Quote:

1 2 3 4 5 6 7 8 9
• October 27th, 2010, 11:44 AM
M7MD
Re: nextint() Method !
Quote:

Originally Posted by JavaPF
I would do it like this:

Code Java:

```import java.util.Random;   public class Numbers {   /** * JavaProgrammingForums.com */ public static void main(String[] args) {   Random r = new Random();   int Number; int Count = 1;   while(Count != 10){   Number = r.nextInt(10)+1;   if(Number == Count){ System.out.print(Number + " "); Count++; } } } }```

Output:

Man !

thanks very much (*)

100% :cool:

and i'am understand the idea in true way :)
• October 27th, 2010, 11:47 AM
M7MD
Re: nextint() Method !
Quote:

Originally Posted by helloworld922
That condition is completely unnecessary. The definition of nextInt() is that it will return an integer in the range [0,10). Simply change your random-number generation statement to this and there's no need for an if statement.

Code Java:

```int number = r.nextInt(9) + 1; // no need for an if statement, number is guaranteed to be in the range [1,10] array[i] = number; // ...other code```

Yes !

thanks for your useful idea (*)