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 3 of 3

Thread: Random sequence

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Random sequence

    Hi,

    I'm trying to create a random sequence of 100 numbers (min 1, max 100) and:

    -- Summarize them (number + number...)
    -- Count them
    -- Pick out the smallest number
    -- Pick out the highest number

    So far it only works WITHOUT randomly picked numbers. How can I make this random?:

    {
    		int start = 1, end = 100, step = 1;					
     
    		int element;		
    		long sum = 0;									
    		int count = 0, min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
     
    		for (int i = start; i <= end; i+=step)				
    		{													
    			element = i;									
    			sum += element;									
    			count++;										
    			min = Math.min(min, element);					
    			max = Math.max(max, element);				
    			System.out.printf("(%d/%d) ", i, element);		
    		}	
     
    		if (count == 0)									
    		{												
    			System.out.printf("Count:%,d, Sum:%,d, Min, Max not available", count, sum);
    		}
    		else
    		{
    			System.out.println();							
    			System.out.printf("Sum: %,d, Count: %,d, Min: %,d, Max: %,d ", sum, count, min, max);
    		}
    	}
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Random sequence

    How can I make this random?
    There are a couple of ways to get random numbers:
    1) The Math class has a method that returns random numbers
    2) The Random class has methods for generating random numbers
    3) Fill a List with sequential numbers and use the Collections class's shuffle method. Guarantees no repeats
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Random sequence

    Also posted here: https://coderanch.com/t/731786/java/...quence#3404683

    Please read: http://www.javaprogrammingforums.com...s-posting.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Generate random numbers between 1 and 52 by passing a seed to Random.
    By Shareefuddin in forum Object Oriented Programming
    Replies: 2
    Last Post: April 22nd, 2013, 09:48 AM