programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# My random integer generator.

Ranch Hand
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

Marshal
Posts: 57435
175
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

Bartender
Posts: 2700
• 1
Why the complex logic? Why not just generate a random number and if it is odd and you want even subtract one?

Aces Kok Ben
Ranch Hand
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
Marshal
Posts: 57435
175

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.

Java Cowboy
Sheriff
Posts: 16079
88

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.

 It is sorta covered in the JavaRanch Style Guide.