This discussion is archived
8 Replies Latest reply: Jan 16, 2013 11:18 AM by abillconsl RSS

What is happening in this code

984451 Newbie
Currently Being Moderated
Hello guys

import java.util.*;



public class nip{
     
     public static void main(String args[]){
     
          Random rn=new Random();
          
          int array1[]=new int[7];
          
          
          for(int counter=1;counter<50;counter++){
               
               ++array1[1+rn.nextInt(3)];
               
     }
          
for(int face=1;face<array1.length;face++){
               
          System.out.println(array1[face]);
               
          }

System.out.println(1+rn.nextInt(6));
          
          
          
          
          
     }}
its give me random 3 digit numbers
How it's works

Thank you guys
  • 1. Re: What is happening in this code
    EJP Guru
    Currently Being Moderated
    How do you think it works?
  • 2. Re: What is happening in this code
    984451 Newbie
    Currently Being Moderated
    i watched some video tutorials and it has this code so i type it and try to understand it
    but it hard to understand how it will give 3 digit numbers
  • 3. Re: What is happening in this code
    EJP Guru
    Currently Being Moderated
    It won't. It seems to me the maximum value that can be printed is 49.
  • 4. Re: What is happening in this code
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Icedesert wrote:
    i watched some video tutorials and it has this code so i type it and try to understand it
    but it hard to understand how it will give 3 digit numbers
    Because of this:
    ++array1[1+rn.nextInt(3)];
    http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt(int)

    BTW: you should forget anything from the video. It's Java the way C(++) programmers do it.

    If you want to learn Java go through the oracle tutorials:
    http://docs.oracle.com/javase/tutorial/

    bye
    TPD
  • 5. Re: What is happening in this code
    rp0428 Guru
    Currently Being Moderated
    >
    Icedesert wrote:
    i watched some video tutorials and it has this code so i type it and try to understand it
    but it hard to understand how it will give 3 digit numbers
    Because of this:
    ++array1[1+rn.nextInt(3)];
    http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt(int)
    >
    Really? How does a function that returns a value between 0 and 3 return a 3 digit number?
    >
    Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
  • 6. Re: What is happening in this code
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    rp0428 wrote:
    Really? How does a function that returns a value between 0 and 3 return a 3 digit number?
    OK, I was guessing, but I think the OP wonders why only 3 different numbers come back (wich, by looking at this code, made more sense for me than asking for three digit numbers...)

    bye
    TPD
  • 7. Re: What is happening in this code
    984451 Newbie
    Currently Being Moderated
    Sorry guys

    It's like this
    import java.util.*;
     
     
     
    public class nip{
         
         public static void main(String args[]){
         
              Random rn=new Random();
              
              int array1[]=new int[7];
              
              
              for(int counter=1;counter<1000;counter++){
                   
                   ++array1[1+rn.nextInt(6)];
                   
         }
              
    for(int face=1;face<array1.length;face++){
                   
              System.out.println(array1[face]);
                   
              }
     
    i didn't understand
              for(int counter=1;counter<1000;counter++){
                   
                   ++array1[1+rn.nextInt(6)];
                   
         }
    what happen i this part
  • 8. Re: What is happening in this code
    abillconsl Explorer
    Currently Being Moderated
    It keeps adding one to any of the array elements between 1 and 6, randomly choosing which element each time X 999.

    Edited by: abillconsl on Jan 16, 2013 2:18 PM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points