Generating Random Numbers

Hi,

I thought this would be the best place to start as I am effectively new to the java language.

I started writing code to generate numbers for the UK lottery. I thought I'd try extend it to make the program generate X random numbers between a min and a max value.

I seem to be getting incorrect random numbers when running this, if I use min 1 and max 49 as the arguments it appears ok.

If I use min 300 and max 400 I get numbers like 578 and 666.

I've spent quite a bit of time researching the Random.nextInt method but would like to know if ive misinterpreted the way that this method works.

Thanks.

PS please feel free to add any comments re code layout or other tips. This is all a great learning experience for me.

import java.util.Random;

import java.util.Arrays;

publicclass ArrayTestApp{

/**

* @param args

*/

publicstaticvoid main(String[] args){

// TODO Auto-generated method stub

ArrayTester test1 =new ArrayTester(1, 49);

test1.getNumbers();

ArrayTest test2 =new ArrayTest(300, 400);

test2.getNumbers();

}

}

class ArrayTester

{

privateint numbers[] =newint [6];

private Random generator =new Random();

public ArrayTester(int min,int max)

{

setNumbers(min, max);

}

publicvoid setNumbers(int min,int max)

{

System.out.printf("Min = %d, Max = %d\n", min, max);

int i = 0;

while (i < numbers.length)//control is less than array length

{

//generate a new number between min and max

int num = min + generator.nextInt(max);

System.out.println("Generated number = " + num);

// call method haveNumber to see if we already have a generated number

// if we do not then add our number to the array

if (!haveNumber(numbers, num))

{

numbers[i] = num;

++i;

}

}//end while

}//end method setNumbers

// add - returns true if the number is already in the array

// or false if the number does not exist

publicboolean haveNumber(int list[],int val)

{

for (int count = 0; count < list.length; count++)

{

if (list[count] == val)

{

returntrue;

}

}

returnfalse;

}

// print out the array of numbers

publicvoid getNumbers()

{

//sort the array

Arrays.sort(numbers);

//print out each number

for (int count = 0; count < numbers.length; count ++)

System.out.printf("Number%d = %d\n" , count+1, numbers[count]);

}// end getNumbers

}// end class ArrayTest

[5196 byte] By [r-loa] at [2007-11-27 10:28:35]
# 1

generator.nextInt(max);

will return a number between 0 and max. The problem happens when you add min to this result as the random number is between 0 - 400.

try with

min + generator.nextInt(max - min);

Cogsya at 2007-7-28 17:52:32 > top of Java-index,Java Essentials,New To Java...
# 2

Rethink the following line:

int num = min + generator.nextInt(max);

hint: 'max' is supposed to be something else...

EvilBroa at 2007-7-28 17:52:32 > top of Java-index,Java Essentials,New To Java...
# 3

package cruft;

import java.util.*;

public class LotteryDrawing

{

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

System.out.print("How many numbers do you need to draw? ");

int k = in.nextInt();

System.out.print("What is the highest number you can draw? ");

int n = in.nextInt();

int[] numbers = new int[n];

for (int i = 0; i < numbers.length; i++)

numbers[i] = i + 1;

int result[] = new int[k];

for (int i = 0; i < result.length; i++)

{

int r = (int) (Math.random() * n);

result[i] = numbers[r];

}

Arrays.sort(result);

System.out.println("Bet the following combination. It'll make you rich!");

for (int r : result)

System.out.println(r);

}

}

output

How many numbers do you need to draw? 6

What is the highest number you can draw? 56

Bet the following combination. It'll make you rich!

3

5

18

29

45

55

fastmikea at 2007-7-28 17:52:33 > top of Java-index,Java Essentials,New To Java...
# 4

Well, I guess thats what learning is all about!

Thanks everybody. (EvilBro is truly evil :-) - thanks for kicking the mind cogs back into life this morning!)

r-loa at 2007-7-28 17:52:33 > top of Java-index,Java Essentials,New To Java...