Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Math.random() with no duplictaes

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Math.random() with no duplictaes

    Help me solve this please. been trying for about 2 hours now. and this is the last piece of code i need for my main program. help would be appreciated. thanks.

    import javax.swing.*;
    public class random 
    {
     
        public static void main(String[]args) 
        {
        	int resultNum [] = {50,50,50,50,50,50};
     
        	for(int x = 0;x<6; x++)
        	{
        		resultNum[x] = 0 + (int)(Math.random() * ((29 - 0) + 1));
     
        		for(int y=5;y>6;y++)
        		{	
        			if(resultNum[x] == resultNum[y])
        			{
        				resultNum[x] = 0 + (int)(Math.random() * ((29 - 0) + 1));
        				break;
        			}
     
        			else
        			{
        				resultNum[x] = resultNum[x];
        				break;
        			}	
        		}
        	}
     
        	JOptionPane.showMessageDialog(null, resultNum[0] + " " + resultNum[1] + " " + resultNum[2] + " " + resultNum[3] + " " + resultNum[4]
        	+ " " + resultNum[5] );
        } 
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Math.random() with no duplictaes

    Can you explain how the program is supposed to recognize a duplicate? What are the steps the program should take to see if a number is a duplicate?

    for(int y=5;y>6;y++)
    Can you explain what the above if statement is supposed to do?
    If y starts at 5 and the test to continue the loop is if y > 6, when will the loop execute?

    Can you post the program's output and explain what is wrong with it.

    BTW you should NOT hardcode values (the 6) in for() statements when iterating through an array. You should use the .length field of the array for testing when at the end of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Location
    Philippines
    Posts
    46
    My Mood
    Cheeky
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Math.random() with no duplictaes

    the program should first generate a random number and then checks the generated number if the generated number is already in the array. and if the generated number is already in the array it will generate another random number.

    that statement is a dummy, because i was to frustrated already and cant think straight.

    here is the output. as you can see. it has 3 number 26s.

    Capture.PNG

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Math.random() with no duplictaes

    checks the generated number if the generated number is already in the array.
    Your list of steps for what the program needs to do leaves out saving the new number in the array.
    The number needs to be saved in the array so future checks of the array will find it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problems with Math.Random() in a for loop
    By csharp100 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2012, 07:18 PM
  2. Random math problem generator?
    By CamCompetes in forum Member Introductions
    Replies: 1
    Last Post: March 9th, 2012, 02:42 PM
  3. Need help understanding math.random method.
    By slashdash in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2012, 05:50 PM
  4. display the generated Math.random number
    By PsYNus in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 2nd, 2011, 11:25 AM
  5. Math.Random()
    By xionyus in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2011, 11:22 PM