Another dump code monkey



  • Important lesson, just because you got most the right pieces, doesn't mean you can put them in any order you like.

    Question:  How do I generate a random integer between 1st integer and
    2nd integer?

    I would like to generate an integer number
    between 1st integer and 2nd integer but I am lost as to where goes
    wrong? My code can generate integer number but not between 1st & 2nd
    integer.
    Attached CODE...
    [CODE]
    import java.util.Scanner;
    import
    java.util.Random;

    public class Ranint {

       public static
    void main(String[] args) {
           Scanner scan = new Scanner (
    System.in );
           Random random = new Random();



           int start = 0, end = 100;

           int number1 =
    random.nextInt( end - start + 1 ) + start;
           int number2 =
    random.nextInt( end - start + 1 ) + start;

       
    System.out.print("Enter first integer: "); //1st Prompt
       start =
    scan.nextInt();

    System.out.print("Enter second integer: ");
    //2nd Prompt
    end = scan.nextInt();


           if (
    number1<number2 )
               System.out.println( number1 );

           else
               System.out.println( number2 );
       }
    }
    [/CODE]



  • Sounds like homework to me but i'm gonna give you a hint:

    Try to generate the random number after getting the numbers from the user.



  •  Err, it's a WTF, not a question. Read the first line 🙂



  • @Mole said:

    Err, it's a WTF, not a question. Read the first line 🙂

    Even if I read this full, I have the attention span of a goldfish so the result could have been more random than that.

    I first developed this ability when i got my first job and my boss would not stop to talk about totaly unrelated things and I had to stay focused on the main content. But now it has spread more like a virus.

    What was I talking about ?



  • @ltouroumov said:

    What was I talking about ?
    Something about goldfish?



  • I think it's a homework question disguised as a WTF.  Who uses console IO with random number generation?



  • The obvious solution is to add another prompt so that the user can enter the desired random number. Then you can add validation to ensure that it's between the start and end numbers.



  • @Scarlet Manuka said:

    The obvious solution is to add another prompt so that the user can enter the desired random number. Then you can add validation to ensure that it's between the start and end numbers.

    OR you could prompt the user for a random number of numbers then pick a random number out of the pool of number and ensure it between bounds and random by running some kind of analysis, all that via call to multiple web services run in parallel threads and a huge database of statistics about random numbers.

    Why settle for a simple solution when you can go for a more complex one ? (I think it's my boss motto)



  • @Scarlet Manuka said:

    The obvious solution is to add another prompt so that the user can enter the desired random number. Then you can add validation to ensure that it's between the start and end numbers.
     

     

    But how will you implement the "...GOTO INPUTNO" statement in case of unacceptable input?



  •  @Scarlet Manuka said:

    The obvious solution is to add another prompt so that the user can enter the desired random number. Then you can add validation to ensure that it's between the start and end numbers.

    No XML?



  • XML no, you have to develop a parser for a new an totaly unknown markup type that's totaly unreadable by humans, only by a crazy monk that speaks a foreign language that nobody understands anymore (I don't know what it is because it doesnt exist).



  • @ltouroumov said:

    XML no, you have to develop a parser for a new an totaly unknown markup type that's totaly unreadable by humans, only by a crazy monk that speaks a foreign language that nobody understands anymore (I don't know what it is because it doesnt exist).
    I suspect a mainframe was involved here...



  • I only described the communication language between the servers. Another crazy monk is in charge of implementation and documentation of the server architecture. The third crazy monk will program the OS and the fourth the HTTP Base API for communication. And 50 clerks will be hired to ensure the data transfer between the servers using state of the art printer-wooden tables-keyboard system (patents pending). Did I mention the drummer and the guy with the whip ?



  • @ltouroumov said:

    a foreign language that nobody understands anymore


    Esperanto? That would be it.



  • @Cad Delworth said:

    @ltouroumov said:

    a foreign language that nobody understands anymore


    Esperanto? That would be it.
     

    If it were in a text file on a Unix machine, I could understand it.



  • @subanark said:

    Important lesson, just because you got most the right pieces, doesn't mean you can put them in any order you like.

    Except for in Haskell:

    import Control.Applicative
    import Control.Monad
    import System.IO
    import System.Random
    
    main = do
    	hSetBuffering stdout NoBuffering
    	number <- join $ getStdRandom . randomR <$> liftA2 (,)
    		(putStr "Enter first integer: " >> (read :: String -> Int) <$> getLine)
    		(putStr "Enter second integer: " >> (read :: String -> Int) <$> getLine)
    	print number
    


  • @Obi-Wan said:

    No XML?

    Ooh, I like this.


    <random_numbers>

        <number>1</number>

        <number>2</number>

        <number>3</number>

        <number>4</number>

        <number>5</number>

        <number>6</number>

        <number>7</number>

        <number>8</number>

        <number>9</number>

        <number>10</number>

     

    </random_numbers>


    Now all you need is an XSLT to select a number, but we'll leave that as an exercise for the reader.


  • @dhromed said:

    @Cad Delworth said:

    @ltouroumov said:

    a foreign language that nobody understands anymore


    Esperanto? That would be it.
     

    If it were in a text file on a Unix machine, I could understand it.

    I get it.



  •  I love it when an inside joke comes together.

     



  • @subanark said:

    Important lesson, just because you got most the right pieces, doesn't mean you can put them in any order you like.

    Question:  How do I generate a random integer between 1st integer and 2nd integer?

    I would like to generate an integer number between 1st integer and 2nd integer but I am lost as to where goes wrong? My code can generate integer number but not between 1st & 2nd integer.
    Attached CODE...


    import java.util.Scanner;
    import java.util.Random;

    public class Ranint {

       public static void main(String[ args) {
           Scanner scan = new Scanner ( System.in );
           Random random = new Random();


           int start = 0, end = 100;

           int number1 = random.nextInt( end - start + 1 ) + start;
           int number2 = random.nextInt( end - start + 1 ) + start;

        System.out.print("Enter first integer: "); //1st Prompt
       start = scan.nextInt();

    System.out.print("Enter second integer: "); //2nd Prompt
    end = scan.nextInt();


           if ( number1<number2 )
               System.out.println( number1 );
           else
               System.out.println( number2 );
       }
    }

     

     

    Here's what you need to do : Step away from Java for a few minutes and write out the steps in Pseudo code, then re-write it in Java:

    Example:
    Get first integer and store value in Variable A
    Get second integer and store value in Variable B

    Make Variable C the difference between variables A and B  ( C = ABS(B - A))  this gives you the actual random number range.

    Make D a random variable between 0 and C -- (Real languages have a built in random number library)

    Return value would be A + D

    There would probably need to be some additional debugging depending on whether or not the range was inclusive of A and B, and whether your languages random function returned values between 0 and 1, or between 1 and C, etc but that should be trivial.

    EX:

    10 INPUT "First Number";A
    20 INPUT "Second Number", B
    30 C = B - A
    40 D = INT((RND(3) * A) + .5)
    50 E = D + A
    60 PRINT E

     



  • @Medezark said:

    10 GOTO 70
    20 PRINT E
    25 GOTO 80
    30 C = B - A
    35 GOTO 50
    40 E = D + A
    45 GOTO 20
    50 D = INT((RND(3) * A) + .5)
    55 GOTO 40
    60 INPUT "Second Number", B
    65 GOTO 30
    70 INPUT "First Number", A
    75 GOTO 60
    80 PRINT "ZE END"

    See now that's programming !



  • @ltouroumov said:

    @Medezark said:
    10 GOTO 70
    20 PRINT E
    25 GOTO 80
    30 C = B - A
    35 GOTO 50
    40 E = D + A
    45 GOTO 20
    50 D = INT((RND(3) * A) + .5)
    55 GOTO 40
    60 INPUT "Second Number", B
    65 GOTO 30
    70 INPUT "First Number", A
    75 GOTO 60
    80 PRINT "ZE END"

    See now that's programming !

    Yeah .. thats modern day programming for you. All it is, is a cut and paste job with minor changes yet still manages to propagate the original bugs


  • I'm not paid enough neither to write good code nor write documentation (and I think it's against company policy, both of those), so I don't do that. I only make bad code look worse.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.