Jump to content

(Java) How to put random numbers into an array?

creativename
Go to solution Solved by rockking1379,

why are you both trying to keep track of the loop position or whatever? use the dot operator. very powerful

	public static void main(String[] args)	{		System.out.println("Starting...");		int size = 20;		int[] array = new int[size];		System.out.println("Array Initialized");		Random gen = new Random();		int min = 1;		int max = 100;		System.out.println("Generating Randoms");		for(int i=0; i<array.length; i++)		{			array[i] = gen.nextInt(max - min) + min;		}		System.out.println("Printing Randoms Forward");		for(int i = 0; i<array.length; i++)		{			System.out.print(String.valueOf(array[i]) + ", ");		}		System.out.println("\nPrinting Randoms Backward");		for(int i=(array.length-1); i>=0; i--)		{			System.out.print(String.valueOf(array[i]) + ", ");		}	}

My code so far is

 

EDITED CODE:

 

import java.util.Random;
 
public class test 
{
    public static void main(String[] args) 
   
{
        int size = 20;
        int[] array = new int[size];
        int loop = 0;
 
       
Random generator = new Random();
       
 
       
for (int i = 0; i < size; i++)        
       
{
            array[i] = generator.nextInt();
            loop++;
        }
        
        
       
for(int i = loop; i == 0; i--)
        {
            System.out.print(array[i] + " ");
        }
    }
}

 

 

not quite sure what to do.

The 2 things i need my program to do is, put 20 random integers into an array, and then print them backwards

 

like

 

The numbers in order are:

21 3 7 17 43 31 19 21 40 2 37 28 15 5 48 35 11 2 0 33

The numbers in reverse order are:

33 0 2 11 35 48 5 15 28 37 2 40 21 19 31 43 17 7 3 21

 

 

Code compiles, but it doesn't print anything.

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

Untested.

import java.util.Random; public class test {    public static void main(String[] args)     {        int size = 20;        int[] array = new int[size];        int loop = 0;         Random generator = new Random();        int min = 1;        int max = 100;         for (int i = 0; i < size; i++)                {            array[i] = generator.nextInt(max-min) + min; //Random number between min (inclusive and 100 (exclusive)            loop++;        }                        for(int i = loop; i == 0; i--)        {            System.out.print(array[i] + " ");        }    }}

TrinityFX


CPU: i5-4440 @ 3.1Ghz RAM: Crucial 1 x 4GB @ 1.6Ghz  MOTHERBOARD: MSI Z87-G41 PC Mate GPU: PCS+ HD 7870 Ghz Edition | CASE: NZXT Source 210 Window | HDD #1: Toshiba 3.5" 500GB HDD #2: Toshiba 2.5" 500GB PSU: Cougar SL500 OS: Windows 8.1 Pro 


 | https://pcpartpicker.com/b/2wccCJ |

Link to comment
Share on other sites

Link to post
Share on other sites

 

Untested

Untested
import java.util.Random;
 
public class test 
{
    public static void main(String[] args) 
    {
        int size = 20;
        int[] array = new int;
        int loop = 0;
 
        Random generator = new Random();
        int min = 1;
        int max = 100;
 
        for (int i = 0; i < size; i++)        
        {
            array = generator.nextInt(max-min) + min;
            loop++;
        }
        
        
        for(int i = loop; i == 0; i--)
        {
            System.out.print(array + " ");
        }
    }
}

 

It compiles without error, but it also outputs nothing lol

 

Where do you think the problem is?

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

 

Where do you think the problem is?

 

Sorry, I may have overlooked the printing size of things. Just replace your current main method with this (Tested).

        int size = 20;        int[] array = new int[size];         Random generator = new Random();        int min = 1;        int max = 100;         for (int i = 0; i < size; i++)                {            array[i] = generator.nextInt(max-min) + min;        }                for(int i = size - 1; i != 0; i--)        {            System.out.print(array[i] + " ");        }

TrinityFX


CPU: i5-4440 @ 3.1Ghz RAM: Crucial 1 x 4GB @ 1.6Ghz  MOTHERBOARD: MSI Z87-G41 PC Mate GPU: PCS+ HD 7870 Ghz Edition | CASE: NZXT Source 210 Window | HDD #1: Toshiba 3.5" 500GB HDD #2: Toshiba 2.5" 500GB PSU: Cougar SL500 OS: Windows 8.1 Pro 


 | https://pcpartpicker.com/b/2wccCJ |

Link to comment
Share on other sites

Link to post
Share on other sites

 

Sorry, I may have overlooked the printing size of things. Just replace your current main method with this (Tested).

        int size = 20;        int[] array = new int[size];         Random generator = new Random();        int min = 1;        int max = 100;         for (int i = 0; i < size; i++)                {            array[i] = generator.nextInt(max-min) + min;        }                for(int i = size - 1; i != 0; i--)        {            System.out.print(array[i] + " ");        }

One last question, the int size is declared 20, but how come the output only shows 19 numbers? Or am I just miscounting? It displays 20 when i make the int size 21, but I'm confused on why it does this.

CPU: Intel Core i7 2600k | Mootherboard: ASUS P8z68v-Pro | GPU: EVGA GTX780Ti 3GB | RAM: Kingston HyperX Genesis 8GB (4GBx2) 1600mhz | PSU: Corsair AX760 | STORAGE: Samsung 840 Pro 512GB | COOLER: Noctua NH-C14 | CASE: Fractal Design Define R4 Pearl Black | Operating SystemWindows 7 Professional 64-bit |

Link to comment
Share on other sites

Link to post
Share on other sites

One last question, the int size is declared 20, but how come the output only shows 19 numbers? 

Just change this 

i != 0

to this

i != -1

Sorry about that :/ made it in a whim. The loop control variable starts at 19 then ends at 0. As a more visually appealing solution, you could do i >= 0.

TrinityFX


CPU: i5-4440 @ 3.1Ghz RAM: Crucial 1 x 4GB @ 1.6Ghz  MOTHERBOARD: MSI Z87-G41 PC Mate GPU: PCS+ HD 7870 Ghz Edition | CASE: NZXT Source 210 Window | HDD #1: Toshiba 3.5" 500GB HDD #2: Toshiba 2.5" 500GB PSU: Cougar SL500 OS: Windows 8.1 Pro 


 | https://pcpartpicker.com/b/2wccCJ |

Link to comment
Share on other sites

Link to post
Share on other sites

why are you both trying to keep track of the loop position or whatever? use the dot operator. very powerful

	public static void main(String[] args)	{		System.out.println("Starting...");		int size = 20;		int[] array = new int[size];		System.out.println("Array Initialized");		Random gen = new Random();		int min = 1;		int max = 100;		System.out.println("Generating Randoms");		for(int i=0; i<array.length; i++)		{			array[i] = gen.nextInt(max - min) + min;		}		System.out.println("Printing Randoms Forward");		for(int i = 0; i<array.length; i++)		{			System.out.print(String.valueOf(array[i]) + ", ");		}		System.out.println("\nPrinting Randoms Backward");		for(int i=(array.length-1); i>=0; i--)		{			System.out.print(String.valueOf(array[i]) + ", ");		}	}
Link to comment
Share on other sites

Link to post
Share on other sites

 

why are you both trying to keep track of the loop position or whatever?

 

It's not keeping track of the position if I'm not reusing a loop control variable that exists in both two loops. I agree that method isn't practical in this situation but using the variable 'size' is already feasible. 

TrinityFX


CPU: i5-4440 @ 3.1Ghz RAM: Crucial 1 x 4GB @ 1.6Ghz  MOTHERBOARD: MSI Z87-G41 PC Mate GPU: PCS+ HD 7870 Ghz Edition | CASE: NZXT Source 210 Window | HDD #1: Toshiba 3.5" 500GB HDD #2: Toshiba 2.5" 500GB PSU: Cougar SL500 OS: Windows 8.1 Pro 


 | https://pcpartpicker.com/b/2wccCJ |

Link to comment
Share on other sites

Link to post
Share on other sites

Why exactly do you want to use another for statement to print out the numbers?
That's just doubling the time. If you specifically want to print out the number you're adding then print it right after adding it to the array.
 

My code so far is
 
EDITED CODE:
 
import java.util.Random;
 
public class test 
{
    public static void main(String[] args) 
    {
        int size = 20;
        int[] array = new int;
        int loop = 0;
 
        Random generator = new Random();
       
 
        for (int i = 0; i < size; i++)        
        {
            array = generator.nextInt();
            loop++;
        }
        
        
        for(int i = loop; i == 0; i--)
        {
            System.out.print(array + " ");
        }
    }
}
 
 
not quite sure what to do.
The 2 things i need my program to do is, put 20 random integers into an array, and then print them backwards
 
like
 
The numbers in order are:

21 3 7 17 43 31 19 21 40 2 37 28 15 5 48 35 11 2 0 33

The numbers in reverse order are:

33 0 2 11 35 48 5 15 28 37 2 40 21 19 31 43 17 7 3 21
 
 
Code compiles, but it doesn't print anything.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×