Hello,

When random generates an output, it cannot show duplicate strings. So for example the output I am getting (randomly) is, "Freddy, Freddy, Jane" when it should be "Freddy, Jane". I have to hardore so I cannot use Sets or ArrayList, Contains or for anything like that. Also no Stringbuilder. I don't know how to fix this. Does anybody know to kindly help me. Thanks in advance

java.util.Random.

public class frogs
{

      
  private static Random nums = new Random();   

  private static String[] names =
  {
   "Freddy", "Mac", "Jane", "Jule"

  };


 public static int Dice()
   { 
      return (names.nums.nextInt(2) + 1);  
   } 


 public String randomNames()
 {
  String tempString = " ";
  int numOfTimes = Dice();
   int dup = 0;
     
         
     for(int i=0 ; i<numOfTimes; i++)
     {
       if (names[i].equals(names[i])) [I]// if two strings are the same then it should 
                                      //not output the string again. 
                                      // I know this loop is wrong but I don't how to  
                                      //fix this [/I]
       {
         i = 0;
       }
         
       tempString = tempString + names[nums.nextInt(names.length)]; 
       tempString = tempString + ",";  
            
     }
     
     return aTemp;
   }
 }
}

additionally - in the instructions it states to help you accomplish this you will need to look at the Javadoc for the String class to find an instance method that returns true if the receiver contains the sequence of char
values contained in the argument as a subsequence.

There is nothing in Random that says that it can not return 2 equal values back to back if the range of numbers it's returning is so small.
If you want 2 sequential numbers to be different, save it, compare against the last and get another one until its different.

NormR1 - I have no idea what you have stated so therefore I don't understand.

Read the doc for the Random class. For example say it returns numbers from say 0 to 999999999.
If you only want a number in the range of 1-3, how will random map the number it generates to the range 1-3. It will take the result in the range of 0-333333333 for 1, 33333333-66666666 for 2 and 66666666-999999999 for 3. Its possible for the first two numbers it returns to be in one of those ranges, say 3333343 and 1112 would be in the first range.

If you don't want two sequential numbers from the random number generator, say 2 and 2.
Then save the value returned: 2 and test the next value against that number. If it is 2 then ask it again for a number. Continue until a new number. Save that number for next time.

For me to understand I really need to see examples (which I cannot find) and I am heading off. Thanks for your replying NormR1.

Write a small program with a loop for say 30 times to printout what "nums.nextInt(2)" returns. You should see what the generated values are.

Something like this..

import java.util.Random;

public class RandomNum 
{
  public static void main(String[] args) 
  {
    Random r = new Random();

    /* 10 random numbers from 1 to 3 */
    System.out.println("30 random numbers from 1 to 2:");

    for (int i = 0; i < 30; i++) {
      System.out.print(r.nextInt(2) + 1 + " ");
    }
  }
}

Output is : 30 random numbers from 1 to 2:
2 1 1 2 1 2 2 2 1 2 2 2 1 1 2 2 1 1 1 1 2 1 2 1 2 2 1 2 2 2

(had a warning about keeping the thread neat and tidy. Is this OK

Just out of curiosity, what is the overall aim of the program?
Are you trying to generate the power set of an array of values and do you have to use Random? At what point will the program have accomplished it's mission?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.