-1

I am trying to generate non-repeating random numbers. Please help me identify the problem in my code and how to fix it.

package number;

public class name {

    public static void main(String[] args) 
    {
        int counter=0;
        boolean flag=true;
        int number=0;
        int a[] = new int[16];
        try
        {
            while(counter<16)
            {
                while(flag)
                {                           
                    number = (int)(Math.random()*16);
                    for(int i = 0; i < 16; i++)
                    if(a[i]==number)
                    {
                        continue;
                    }
                    else
                    {   
                        System.out.println(""+i+ "===="+ number);
                        a[counter]=number;  
                        flag= false;
                    }       
                }
                for(int i1=0;i1<16;i1++)
                {
                    for(int j=0;j<16;j++)
                    {
                        if(a[i1]==a[j])
                        {
                        }
                        else
                            System.out.print(" \t "+a[i1]);
                    }
                }
            }
            counter --;
        }
        catch(Exception e)
        {
            e.getStackTrace();
        }
    }
}
William Price
  • 4,033
  • 1
  • 35
  • 54
  • 1
    It's unclear what you're trying to accomplish. What are you having trouble with, and more specifically, what do you mean by "non repeating"? – loafy Feb 18 '15 at 04:30
  • 1
    I'm reminded of a [Dilbert: Tour of Accounting](http://dilbert.com/strip/2001-10-25) and [xkcd: 221](http://www.xkcd.com/221/). – Elliott Frisch Feb 18 '15 at 04:31
  • Related (different language, but principles are the same): http://stackoverflow.com/questions/693880/create-random-number-sequence-with-no-repeats?rq=1 – William Price Feb 18 '15 at 04:34
  • There is repeating the (Random)number. –  Feb 18 '15 at 04:34
  • There are multiple problems with your code. I'm not sure what the double `for` loop is intended to accomplish, but it is almost certainly not doing what you want it to do, and is probably in the wrong place. However, I think the best thing is for you to debug the code yourself, either by using a debugger or by putting some `System.out.println` calls in the code to see if the variables are what you think they should be at certain key points. (You may need to know about [`Arrays.toString`](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#toString-int:A-)). – ajb Feb 18 '15 at 04:40
  • it hangs when i run it. –  Feb 18 '15 at 04:45
  • You need to think more about what you're trying to do. A sequence that is constrained to not repeat is by definition *not* random. – Kevin Krumwiede Feb 18 '15 at 05:22
  • You want to sort the numbers from 1-16 in random order? – eckes Feb 18 '15 at 05:48

1 Answers1

2

You're changing the counter variable outside the while(counter<16) loop and you should increment counter counter++ instead of decrementing it.

Titus
  • 22,031
  • 1
  • 23
  • 33