CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2016
    Posts
    2

    Help with random number?

    hi i am trying out random numbers but stuck on something, please can someone help?

    i have got a simple program that gives out random names, when i try to output several at the same time they come out the same. can i change it?
    Code:
    package coral;
    
    public class namegen {
    	public static void main(String args[]) {
    		String[]names;
    		names = new String[10];
    		names[0] = "ben";
    		names[1] = "john";
    		names[2] = "chris";
    		names[3] = "matt";
    		names[4] = "jack";
    		names[5] = "alex";
    		names[6] = "patric";
    		names[7] = "sam";
    		names[8] = "mike";
    		names[9] = "andy";
    		
    		int num = (int) (Math.random()*10);
    		
    		System.out.println("My name is " + names[num]);
    		System.out.println("My name is " + names[num]);
    		System.out.println("My name is " + names[num]);
    		System.out.println("My name is " + names[num]);
    	}
    
    }
    My name is alex
    My name is alex
    My name is alex
    My name is alex

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Help with random number?

    they come out the same. can i change it?
    Was this posted and answered on another forum?
    Norm

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

Featured