Click here to Skip to main content
14,301,527 members
Rate this:
Please Sign up or sign in to vote.
See more:
code that prints random contents The question is code should print distinct array contents but the code here prints same word twice sometimes

import java.security.SecureRandom;

public class JavaArrays 
{                        
    private static final String[] names = {"Word", "My", "Name", "Is", "Android"};
    private static SecureRandom random = new SecureRandom();
        
    public static void main(String[] args) 
    {       
        
        for (int i = 0; i < 4; i++) 
        {
            String randomy = (names [random.nextInt(names.length)]);
            System.out.println("The array is " + randomy);                                    
        }
    }         
}


What I have tried:

coded java that prints random array contents
Posted
Updated 30-Aug-19 5:21am
v4
Comments
OriginalGriff 28-Aug-19 11:33am
   
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
four systems 28-Aug-19 11:51am
   
improved the question
Richard MacCutchan 28-Aug-19 12:14pm
   
If you use random values to choose what to print then there may well be duplicates. If you want to print only unique items then you need to keep a list of previous values and check each time that the new number has not been used before.
four systems 29-Aug-19 9:44am
   
thancs
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
code that prints random contents The question is code should print distinct array contents but the code here prints same word twice sometimes

The problem is your expectation, 'random' means random, and repeating the same word twice is a consequence of random. you can even have a run with the same word every times.
In order to get 'non repeat' drawing of words, you need to resort to shuffling procedure:
Shuffling - Wikipedia[^]
Fisher–Yates shuffle - Wikipedia[^]
Just like with a deck of cards, the 'non repeat' is gained because each card drawn is removed from the deck on following draws.
   
v2
Comments
four systems 29-Aug-19 9:44am
   
that sounds good would try that
Rate this:
Please Sign up or sign in to vote.

Solution 2

Given the number of questions you have posted on basic Java, I would strongly suggest you go to The Java™ Tutorials[^] and spend some solid learning time.
   
Comments
four systems 29-Aug-19 9:45am
   
thancs do that a lot but need more flying hours
Richard MacCutchan 29-Aug-19 9:54am
   
Then please use those flying hours to do proper learning. You will not learn anywhere near as well by posting basic questions here.
four systems 29-Aug-19 10:01am
   
thancs
Rate this:
Please Sign up or sign in to vote.

Solution 3

Quote:
code that prints random contents The question is code should print distinct array contents but the code here prints same word twice sometimes

That's what random means - the next number takes no notice of the previous sequence of numbers and selects a value at random from the total space. That means that it's quite possible to get a number of identical values in a row, especially in small phase spaces. Since you have only 5 items, the odds of getting two identical values in a row are 1/5, but it's quite possible to get 100 "My" indexes in a row, just as you can get 20 "black" calls in roulette (so the "double each time you lose" strategy can get very, very expensive quickly!)

If you want "random but no repeats", you need to copy the input array, scramble it by swapping random elements, then pull them out one by one until there are none left. You then set up a new array using all values except the last used, and scramble that and repeat.
   
Comments
four systems 29-Aug-19 9:47am
   
though there is a mathematical formula for that permutation and combinations sequence and series or factorials thats what the search was for thancs
Rate this:
Please Sign up or sign in to vote.

Solution 4

the code here prints different array contents

public class Shuffle {
   public static void main(String args[]) {
      
      // create array list object       
      List arrlist = new ArrayList();

      // populate the list
      arrlist.add("Android");
      arrlist.add("Computer Science");
      arrlist.add("Satellite Navigation");  
      arrlist.add("Hyan");  

      System.out.println("Initial collection: "+arrlist);

      // shuffle the list
      Collections.shuffle(arrlist);

      System.out.println("Final collection after shuffle: "+arrlist);
   }    
} 
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100