pleeese help me i am sooo stuck

i am in java class in college i have to make a prog theat generates 6 random

no's between 1 and 49 like the lotto.... i use Jcreator to build, i know

this is nothing to you guys probably but so far this is as far as i am and

i have no idea where to start

pleese email me with some help if you can it will be appreciated...

Re: pleeese help me i am sooo stuck

hey, I'm a beginner too! wadyaknow.

look at the Math class and its random() method.

a for loop to generate the numbers

and an array to store the values in and check against for repeats of numbers.

Re: pleeese help me i am sooo stuck

Hey Christopher,

For generating the numbers you'll need 2 items to use in conjunction with

the Math.random() method.

1) a mutiplier

2) a lower bound indicator

You know that Math.random() will return a number in the range of 0 inclusively,

and 1 exclusively. i.e. from 0 to 0.9999999 blah blah blah.

So let's look at some test cases..

Case 1:

I want to generate a number between 0 and 9 inclusively. Looking at what

the Math.random() method gives me, I can see that I can mulitply the random

number by 10. (cast to an int to chuck the fractional)

randNum = (int)(Math.random() * 10);

Math.random() will return values like 0.01, 0.4123, 0.8654, and 0.9159 -

multiplying by 10 will give me a number between 0 and 9 and casting will

truncate the unwanted fractional.

Great, but what if want to generate a number between 1 and 9 inclusively?

Case 2:

So now my lower bound has changed... if

randNum = (int)(Math.random() * 10);

gives me a number between 0 and 9, maybe I can tack on a 1 to this result:

randNum = (int)(Math.random() * 10 + 1);

Even if I get a randomly generated value of 0.01234, multiplying by 10 will

yield 0.1234. Adding 1 will result in 1.1234 and casting to an int will give

me a 1.

Good stuff. I'll never get a 0 assigned to randNum. If I add 3 as in:

randNum = ((int)Math.random() * 10) + 3;

I know I'll get a number that is at least 3, and not lower. Try it just to

verify. We will call this addition the lower bound indicator.

But wait! What if 0.9432 is return by Math.random()? Multiplying by 10 will

yield 9.432. Adding 1 will give 10.432 and casting to and int will return

10. hmmm... not the intended results for our case.

I guess, the easy fix (i.e. trial and error) subtracting 1 from the multplier

will solve our problem.

Therefore

randNum = (int)(Math.random()*9 + 1);

will give us our number between 1 and 9.

Actually, this is all you need for your assignment. You can call 9 your upperbound

for your assignment, but it really isn't by the way we derived it here. (the

following is just an extention and wrap up if you're curious as to why I

didn't call my multiplier the upper bound)

So we said we needed a multiplier and and a lower bound indicator. We've

verified the lower bound indicator will return a value greater than or equal

to the specified minimum value. But what about the multiplier? Is it safe

to assume the multiplier is the upper bound? Let's see..

Case 3:

Let's try to generate 20 values between -100 and 100 using what we know:

randNum = (int)(Math.random()* 100 - 100);

Math.random() will return values like 0.01, 0.1234, 0.578, and 0.9843 etc.

Multiplying the above by 100 will result in 1, 12.34, and 98.43. Hmm...something's

fishy about that last value. The highest value Math.random will return is

.999999 blah blah blah. So if we assume the multiplier is our "upper bound",

multiplying by 100 will wive us 99.999999 blah blah blah. But wait! We didn't

factor in the lower bound. We kinda need that to assure we get a minimum

value. Subtracting 100 will give us -0.0.....1. To reiterate the subtraction

is imperative. So if Math.random() * 100 will give us a max of 99.99999 blah

blah blah, the following subtraction will give us an "upper bound" of 0.0...01.

Not the intended results. What ever shall we do?

First of all let's take care of the multiplication before the sutraction

occurs. I originally wanted a value between -100 and 100. I want that 100

dangit! ( [ed] I'm getting tired, sorry) So let's add 1 to the multiplier

first of all to get:

randNum = (int)(Math.random()* 101 - 100);

This assures the mulitplication of the max of Math.random() = 0.999 blah

blah blah blah and 101 will yield a value like 100.989 and casting to int

will give that 100 that I want. But remember, the following subtraction -

it will still give me an "upper bound" of 0. So hey, let's add again to that

multiplier, this time by 100 to get:

randNum = (int)(Math.random()* 201 - 1);

This time if we get the good ol' max of 0.99999 blah blah blah from Math.random()

and multiply it by 201, we get something like 200.989 blah blah blah. Subtracting

our lower bound will give us that elusive upper bound of 100.

What did we do then to our multiplier? We added 1 to inch up to a value before

subtraction occured, and we added another 100. What really did was create

a multiplier that is the upper bound minus the lower bound + 1, and for clarity:

multiplier = uBound- lBound + 1;

In our case, it was

100 - (-100) + 1

=200 + 1

=201

So the multiplier isn't really much of an upperbound, as it is a range.

So the items you need for generating a number between 2 numbers is

1) the multiplier, representing the range of values (+1 to include 0)

2) a lower bound

Plugging it into an expression using the Math.random method, we get

randNum=(int)(Math.random() * (upperBound - lowerBound + 1) - lowerbound);

Now I can enjoy my Sunday morning :p

Hope this helps,

Roly

P.S. This is my second time posting on Devx and/or the java.general NG. I

sincerely hope I have adhered to the rules of posting.

"christopher" <chris_tt99@hotmail.com> wrote:

>

>i am in java class in college i have to make a prog theat generates 6 random

>no's between 1 and 49 like the lotto.... i use Jcreator to build, i know

>this is nothing to you guys probably but so far this is as far as i am and

>i have no idea where to start

>

>

>pleese email me with some help if you can it will be appreciated...

Re: pleeese help me i am sooo stuck

Hey Christopher,

For generating the numbers you'll need 2 items to use in conjunction with

the Math.random() method.

1) a mutiplier

2) a lower bound indicator

You know that Math.random() will return a number in the range of 0 inclusively,

and 1 exclusively. i.e. from 0 to 0.9999999 blah blah blah.

So let's look at some test cases..

Case 1:

I want to generate a number between 0 and 9 inclusively. Looking at what

the Math.random() method gives me, I can see that I can mulitply the random

number by 10. (cast to an int to chuck the fractional)

randNum = (int)(Math.random() * 10);

Math.random() will return values like 0.01, 0.4123, 0.8654, and 0.9159 -

multiplying by 10 will give me a number between 0 and 9 and casting will

truncate the unwanted fractional.

Great, but what if want to generate a number between 1 and 9 inclusively?

Case 2:

So now my lower bound has changed... if

randNum = (int)(Math.random() * 10);

gives me a number between 0 and 9, maybe I can tack on a 1 to this result:

randNum = (int)(Math.random() * 10 + 1);

Even if I get a randomly generated value of 0.01234, multiplying by 10 will

yield 0.1234. Adding 1 will result in 1.1234 and casting to an int will give

me a 1.

Good stuff. I'll never get a 0 assigned to randNum. If I add 3 as in:

randNum = ((int)Math.random() * 10) + 3;

I know I'll get a number that is at least 3, and not lower. Try it just to

verify. We will call this addition the lower bound indicator.

But wait! What if 0.9432 is return by Math.random()? Multiplying by 10 will

yield 9.432. Adding 1 will give 10.432 and casting to and int will return

10. hmmm... not the intended results for our case.

I guess, the easy fix (i.e. trial and error) subtracting 1 from the multplier

will solve our problem.

Therefore

randNum = (int)(Math.random()*9 + 1);

will give us our number between 1 and 9.

Actually, this is all you need for your assignment. You can call 9 your upperbound

for your assignment, but it really isn't by the way we derived it here. (the

following is just an extention and wrap up if you're curious as to why I

didn't call my multiplier the upper bound)

So we said we needed a multiplier and and a lower bound indicator. We've

verified the lower bound indicator will return a value greater than or equal

to the specified minimum value. But what about the multiplier? Is it safe

to assume the multiplier is the upper bound? Let's see..

Case 3:

Let's try to generate 20 values between -100 and 100 using what we know:

randNum = (int)(Math.random()* 100 - 100);

Math.random() will return values like 0.01, 0.1234, 0.578, and 0.9843 etc.

Multiplying the above by 100 will result in 1, 12.34, and 98.43. Hmm...something's

fishy about that last value. The highest value Math.random will return is

.999999 blah blah blah. So if we assume the multiplier is our "upper bound",

multiplying by 100 will wive us 99.999999 blah blah blah. But wait! We didn't

factor in the lower bound. We kinda need that to assure we get a minimum

value. Subtracting 100 will give us -0.0.....1. To reiterate the subtraction

is imperative. So if Math.random() * 100 will give us a max of 99.99999 blah

blah blah, the following subtraction will give us an "upper bound" of 0.0...01.

Not the intended results. What ever shall we do?

First of all let's take care of the multiplication before the sutraction

occurs. I originally wanted a value between -100 and 100. I want that 100

dangit! ( [ed] I'm getting tired, sorry) So let's add 1 to the multiplier

first of all to get:

randNum = (int)(Math.random()* 101 - 100);

This assures the mulitplication of the max of Math.random() = 0.999 blah

blah blah blah and 101 will yield a value like 100.989 and casting to int

will give that 100 that I want. But remember, the following subtraction -

it will still give me an "upper bound" of 0. So hey, let's add again to that

multiplier, this time by 100 to get:

randNum = (int)(Math.random()* 201 - 1);

This time if we get the good ol' max of 0.99999 blah blah blah from Math.random()

and multiply it by 201, we get something like 200.989 blah blah blah. Subtracting

our lower bound will give us that elusive upper bound of 100.

What did we do then to our multiplier? We added 1 to inch up to a value before

subtraction occured, and we added another 100. What really did was create

a multiplier that is the upper bound minus the lower bound + 1, and for clarity:

multiplier = uBound- lBound + 1;

In our case, it was

100 - (-100) + 1

=200 + 1

=201

So the multiplier isn't really much of an upperbound, as it is a range.

So the items you need for generating a number between 2 numbers is

1) the multiplier, representing the range of values (+1 to include 0)

2) a lower bound

Plugging it into an expression using the Math.random method, we get

randNum=(int)(Math.random() * (upperBound - lowerBound + 1) - lowerbound);

Now I can enjoy my Sunday morning :p

Hope this helps,

Roly

P.S. This is my second time posting on Devx and/or the java.general NG. I

sincerely hope I have adhered to the rules of posting.

"christopher" <chris_tt99@hotmail.com> wrote:

>

>i am in java class in college i have to make a prog theat generates 6 random

>no's between 1 and 49 like the lotto.... i use Jcreator to build, i know

>this is nothing to you guys probably but so far this is as far as i am and

>i have no idea where to start

>

>

>pleese email me with some help if you can it will be appreciated...

Re: pleeese help me i am sooo stuck

The nextInt() method in the Random class define under java.util.Random would

return a random integer number. nextInt() has a overload form that would

accept a upperbound value. Well, in your case...

class someClass1

{

public static void main(String args[])

{

Random ran = new Random();

for(int i=1; i<=6; i++)

System.out.print(ran.nextInt(48)+1+" ");

}

}

ran.nextInt(48) return a value whithin the range 0 to 48.

"christopher" <chris_tt99@hotmail.com> wrote:

>

>i am in java class in college i have to make a prog theat generates 6 random

>no's between 1 and 49 like the lotto.... i use Jcreator to build, i know

>this is nothing to you guys probably but so far this is as far as i am and

>i have no idea where to start

>

>

>pleese email me with some help if you can it will be appreciated...