Jump to content

An array of random numbers

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
ld_pvl

ld_pvl

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
Hi,

Can anyone help me with this problem please. Say I have a 1 dimensional array with 15 spaces:


int[] Array = new int[15]


Now I want to generate random numbers and put them randomly into that Array, but the numbers must be from 1 to 15, and no numbers should repeat.

Thanks

#2
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

ld_pvl said:

Hi,

Can anyone help me with this problem please. Say I have a 1 dimensional array with 15 spaces:


int[] Array = new int[15]


Now I want to generate random numbers and put them randomly into that Array, but the numbers must be from 1 to 15, and no numbers should repeat.

Thanks

Fun thing to do, I will help you since you might have a trouble to figure it out !

public class trythis2 {

	public static void main(String[] arg) {

	

		int[] Array = new int[15];

		

		readIn(Array);

		

		for(int i=0; i<15; i++) {

			System.out.println("The array will contain"+Array[i]);

		}

	}

	public static void readIn(int[] List) {

		for(int j=0; j<List.length; j++) {

			List[j] = (int) (Math.random()*15+1);

		}

	}

}
I would leave the number repeating thing to you :)
I recommend you do an second method that will allow you to set an exception on the array, if array contains same value but in different position, run again within the selected positions.
Good Luck !
Posted Image

#3
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Even if you seed the Random Generator with some value it is possible to get a repeated value (from my c knowledge). It is still possible if you use an offset. What i would do is store each number i generate in some array or storage structure, so as i generate,i check storage, if not i add it to Array[i], if it exist generate again and repeat accordingly.

#4
Dr. Xi

Dr. Xi

    Newbie

  • Members
  • Pip
  • 6 posts
Interesting question.

public class Random {
    public static void main(String[] args) {
        int[] a = new int[15];
        initializeArray(a);
        randomizeArray(a);

        // check 
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]);
            if (i != (a.length - 1)) {
                System.out.print(", ");
            }
        }
        System.out.println();
    }

    private static void initializeArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
            a[i] = i + 1;
        }
    }

    private static void randomizeArray(int[] a) {
        for (int j = (a.length - 1); j >= 0; j--) {
            int random = (int) (Math.random() * 15);
            if (random != j) {
                int tmp = a[j];
                a[j] = a[random];
                a[random] = tmp;
            }
        }
    }
}