Lilspidr
Lilspidr

Reputation: 11

java random number generator without arrays

I am completely new to Java and working on an assignment. I need to generate 6 random numbers within the range of 1 - 49, which is simple. However, the complication is that the numbers cannot duplicate. The only options I have for conditionals are the while loop, or the if else statements. Arrays and all other loops are off limits for this assignment. Could really use some advice regarding logistics of how to apply the options I have. I realize without arrays, and the other loops that I'm going to have a lot of duplicate code but can handle that. I just need some help wrapping my head around this.

Upvotes: 1

Views: 1038

Answers (5)

Cesare Fischetti
Cesare Fischetti

Reputation: 111

If you can't use arrays, or other collections, you can create 6 variables to save your generate numbers.

public class Generate6Number {

public static void main(String[] args) {
    int num1 = 0;
    int num2 = 0;
    int num3 = 0;
    int num4 = 0;
    int num5 = 0;
    int num6 = 0;

    int count = 0;

    while (count < 6) {
        int numGen = (int) (Math.random() * 49);

        if (numGen != num1 &&
            numGen != num2 &&
            numGen != num3 &&
            numGen != num4 &&
            numGen != num5) {
            if (num1 == 0)
                num1 = numGen;
            else if (num2 == 0)
                num2 = numGen;
            else if (num3 == 0)
                num3 = numGen;
            else if (num4 == 0)
                num4 = numGen;
            else if (num5 == 0)
                num5 = numGen;
            else 
                num6 = numGen;

            count++;
        }
    }   

    System.out.println("Number 1: " + num1);
    System.out.println("Number 2: " + num2);
    System.out.println("Number 3: " + num3);
    System.out.println("Number 4: " + num4);
    System.out.println("Number 5: " + num5);
    System.out.println("Number 6: " + num6);

}

}

Upvotes: 4

Lajos Arpad
Lajos Arpad

Reputation: 76444

You effectively need to take out the already chosen possibility:

Random rand = new Random();
int first = rand.nextInt(49) + 1;
int second = rand.nextInt(48) + 1;
int third = rand.nextInt(47) + 1;
int fourth = rand.nextInt(46) + 1;
int fifth = rand.nextInt(45) + 1;
int sixth = rand.nextInt(44) + 1;

if (first <= second) second++;
if (first <= third) third++;
if (first <= fourth) fourth++;
if (first <= fifth) fifth++;
if (first < sixth) sixth++;

if (second <= third) third++;
if (second <= fourth) fourth++;
if (second <= fifth) fifth++;
if (second <= sixth) sixth++;

if (third <= fourth) fourth++;
if (third <= fifth) fifth++;
if (third <= sixth) sixth++;

if (fourth <= fifth) fifth++;
if (fourth <= sixth) sixth++;

if (fifth <= sixth) sixth++;

Upvotes: 0

Andy Turner
Andy Turner

Reputation: 140319

This seems like a rather strange exercise to attempt without arrays. It is possible, but I don't think it is especially instructive for learning Java, as it is more a maths exercise than a programming one.

In order to generate 6 distinct random numbers in the range 1-49, you basically have to decide for each of those 49 numbers whether or not to "pick" it, with the constraint that you will only pick 6 numbers. So, you are looking to identify one of the choose(49, 6) possible combinations.

So, you can do it like this:

int numToPick = 6;
for (int i = 1; i <= 49; ++i) {
  if (shouldPick(i, numToPick)) {
    System.out.println(i);
    --numToPick;
  }
}

(I used a for loop; but you can always rewrite a for loop as a while loop if that's all you can use).

The question is just how to implement that shouldPick function: it returns a boolean true to mean that you should pick it, and will print it; or false to mean that you won't.

This is going to be a probabilistic function (i.e it's going to use a random number generator), or it won't give you different sets of numbers each time.

The maths to work out the form of that function is quite fun, and the ultimate implementation is pleasingly trivial; but it wouldn't be fun if I gave the answer here.

(But I can assure you that I have implemented it, only changing one line in the above snippet; 2 if you count declaring a random number generator).

Upvotes: 0

ernestk
ernestk

Reputation: 72

like shuffling a card for first 6

public void shuffle() {
    int[] array = new int[49];
    for (int i = 0; i < array.length; i++) {
        array[i] = i + 1;
    }

    // we need first 6 random cards
    for (int i = 0; i < 6; i++) {
        int index = (int) (Math.random() * (( array.length - i) + 1));
        int temp = array[i];
        array[i] = array[index];
        array[index] = temp;
    }

    for (int i = 0; i < 6; i++) {
        System.out.print("[" + array[i] + "]");
    }
    System.out.println();
}

Upvotes: 0

Centos
Centos

Reputation: 260

Set<Integer> values = new HashSet<>();
Random rand = new Random();

while (values.size() <= 6) {
    int  n = rand.nextInt(50) + 1;
    values.add(n);
}
System.out.println(values);

Upvotes: 0

Related Questions