• Post Reply Bookmark Topic Watch Topic
  • New Topic

My random integer generator.  RSS feed

 
Aces Kok Ben
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!