Results 1 to 4 of 4

Thread: random with arrays

  1. #1

    Thread Starter
    Addicted Member annie613's Avatar
    Join Date
    May 2003
    Location
    NY, NY
    Posts
    161

    random with arrays

    i think i have my logic wrong

    i have 2 arrays that generate random cities and post the cities to my applet...
    this is all working fine..

    but sometimes the applet will randomly generate two of the same cities
    like: Miami Miami

    here is my code
    and i know my logic is wrong in my if loop

    or maybe i dont need that index and index1 for the same array???

    any suggestions
    thanks in advance annie

    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    
    public class myAppletTest extends JApplet
    {   
        private String arrLocal[] = new String[5];
        private String arrForeign[] = new String[3];
         
        Random rnd = new Random();
        int index = rnd.nextInt( arrLocal.length );
        String randomLocal = arrLocal[index];
            
        Random rnd1 = new Random();
        int index1 = rnd.nextInt( arrLocal.length );
        String randomLocal1 = arrLocal[index1];
        
        Random rnd2 = new Random();
        int index2 = rnd.nextInt( arrForeign.length );
        String randomForeign = arrForeign[index2];
        
        public void init()
        {
            arrLocal = new String[] { "New York", "Boston", "Miami", "Philadelphia", "Chicago" };
            arrForeign = new String[] {"Madrid", "London", "Paris"}; 
                   
            for (int k = 0; k < arrLocal.length; k ++)
                System.out.println(arrLocal[k]);
               
            for (int i = 0; i < arrForeign.length; i ++)
                System.out.println(arrForeign[i]);
                
            if(arrLocal[index]==arrLocal[index1])
            {
                for (int k = 0; k < arrLocal.length; k ++)
                System.out.println(arrLocal[k]);        
            }
            
        }//end init()
        
        public void paint(Graphics g)
        {
            g.drawString( "We have special rates to: " , 5 , 30);
            g.drawString( arrLocal[index] , 5 , 45);
            g.drawString( arrLocal[index1] , 5 , 60);
            g.drawString( "Inexpenive vacations to: " , 5 , 90);
            g.drawString( arrForeign[index2] , 5 , 105);   
              
        }
    }
    'nothing in the universe is as diverse as a byte of data'

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: random with arrays

    I kind of doubt it's your logic..In my experience, the Random object wasn't completly random...However, you could make it to were the same thing is not selected twice in the arrays..Also, I think there is something called a 'seed' of the random class that makes numbers more random...

  3. #3

    Thread Starter
    Addicted Member annie613's Avatar
    Join Date
    May 2003
    Location
    NY, NY
    Posts
    161

    Re: random with arrays

    thanks...
    this is what i came up with

    Code:
    Random rnd = new Random();                             
    int index = rnd.nextInt(arrLocal.length);
            
    String randomLocal = arrLocal[index];
    String nextOne = null;                           
            
    while((nextOne = arrLocal[rnd.nextInt(arrLocal.length)]).equals(randomLocal));
            
    Random rnd2 = new Random();                          
    int index2 = rnd2.nextInt(arrForeign.length);
    String randomForeign = arrForeign[index2];
            
    g.drawString( "We have special rates to: " , 5 , 30); 
    g.drawString( randomLocal, 5 , 45); 
    g.drawString( nextOne, 5 , 60);                  
    g.drawString( "Inexpenive vacations to: " , 5 , 90); 
    g.drawString( arrForeign[index2] , 5 , 105);
    'nothing in the universe is as diverse as a byte of data'

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: random with arrays

    looks good. Definatley a way of doing it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width