lundi 26 mars 2018

Create a simple java random number pick with in a certain range

I tried searching on here for something similar, but failed to find it, maybe using wrong keywords let me know, but here is the deal.

I am fairly new with java and wanted to make something useful myself. My idea was to create a random number picker within a range with. So let's say range is from 1-50, and I want 5 random number in this range, and they have to be all different. I have worked with Random before, but not sure what I am doing wrong, here is the code I have so far, please push me in the right direction if possible. I want to create an array or list with the number, or is there a better way to do this?

import java.util.Scanner;
import java.util.Random;

public class Randomizer {
    static Random rnd = new Random();
    static int rnd(int a, int b){
        return a+rnd.nextInt(b-a+1);
    }

    public static void nPicker(){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter start of range: ");
        int start = sc.nextInt();
        System.out.println("Enter end of range: ");
        int end = sc.nextInt();
        System.out.println("Enter amount of numbers to pick: ");
        int c = sc.nextInt();
        sc.close(); 
        rnd(start,end);
        int[] randomArrays = new int[c];

        for(int i = 0; i>randomArrays.length; i++){
            randomArrays.add();
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        nPicker();
    }

}

sorry if my code is messy.




Aucun commentaire:

Enregistrer un commentaire