Check out Manning's Countdown to 2014. Use discount code crdotd14 all month for 50% off every deal.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# My random integer generator.

Aces Kok Ben
Ranch Hand

Joined: Apr 30, 2011
Posts: 44
Hello guys it's me again. I'm working on this project recently and I wanted to created a algorithm that can randomly generate many sets of integer, however the codes kinda "stuck" somehow. Please take a look at my code:

First time when I run the program on eclispe, the output was:

"This is Main.
(TESTING)First Position: 4
firstPos: 4
TESTING"

I was fascinated by the output. It was supposed to generate another number (...Odd Number generated at second position: + gen2), which is an Odd number for the second position, however, the program stuck there? It should generate an integer that is Odd. I gave another chance and ran the program several times again, this time the output was:

"This is Main.
(TESTING)FirstPos value: 4
Odd Number generated at second position: 9
TESTING"

This is the correct output that I was expecting but why is it working good at times and not at other times? What I was expecting was:

1. If firstPos (an integer), is bigger than 5, than the number generated at the second position should be an ODD no.
2. else, it should be an EVEN number.

What happen? Is my logic flow correct? I spend quite sometime on this because Im not good in programming and now it's screwing me up
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 34513

13
If there is any relation between the first and second digits, then you are not generating random numbers.

Never, never, never try using == true and == false. You get some nasty errors if you mistakenly write = for ==. As you have done in two places . . .
Don't write if ( . . . ) b = true; else b = false; WriteThe & 1 tests whether the last bit in the number is 1, and is a much quicker way to test for odd and even numbers. it only works for & 1, & 3, & 7, etc, and unlike % will return a positive result from negative arguments.
Why have you got booleaen values odd and even? You should not have both, since ∀iodd == !even
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Why the complex logic? Why not just generate a random number and if it is odd and you want even subtract one?

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Aces Kok Ben
Ranch Hand

Joined: Apr 30, 2011
Posts: 44
Campbell Ritchie wrote:If there is any relation between the first and second digits, then you are not generating random numbers.

Never, never, never try using == true and == false. You get some nasty errors if you mistakenly write = for ==. As you have done in two places . . .
Don't write if ( . . . ) b = true; else b = false; WriteThe & 1 tests whether the last bit in the number is 1, and is a much quicker way to test for odd and even numbers. it only works for & 1, & 3, & 7, etc, and unlike % will return a positive result from negative arguments.
Why have you got booleaen values odd and even? You should not have both, since ∀iodd == !even

I wanted to write a class to create a unique ONE-TIME-PIN. And yes, now i realised: it should be == instead of =
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 34513

13
Aces Kok Ben wrote:[ . . . it should be == instead of =
No, it should be nothing at all. If you want it to be false, you write do { ... } while (!done); or if (!boo) { ... } or similar.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 13540

6

Wouter Oet wrote:Why the complex logic? Why not just generate a random number and if it is odd and you want even subtract one?

Or, if you want only even random numbers, just generate a random number and multiply it by 2.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala

Don't get me started about those stupid light bulbs.

subject: My random integer generator.