By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Randomize Numbers From N to M - Java Task

+5 votes

Write a program that takes as input two integers N and M, and randomizes the numbers between them. Note that M may be smaller than or equal to N:

randomize numbers from n to m in Java

asked in Java category by user john7
edited by user golearnweb

1 Answer

+2 votes
Best answer

Here is the solution for this taks, mate:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Pr_7_RandomizeNumbersFromNToM {
    public static void main(String[] args) {
        Scanner console = new Scanner(;
        int n = console.nextInt();
        int m = console.nextInt();

        int maxNumber = 0;
        int minNumber = 0;

        if (n > m) {
            maxNumber = n;
            minNumber = m;

        } else if (n < m) {
            maxNumber = m;
            minNumber = n;

        } else {

        List<Integer> numbers = new ArrayList<>();

        for (int i = minNumber; i <= maxNumber; i++) {
        for (int item : numbers) {
answered by user richard8502
edited by user golearnweb