Math.Random()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tripmaker
    New Member
    • Jun 2007
    • 60

    Math.Random()

    Hi! I'm new at Java and enjoying what I've learned to date. I'm busy designing a GUI (for customers of a Video store). My question is: I want to use Math.random() to generate a unique customer no. The problem is I'm using a TextField and the error that keeps coming up is that I'm unable to use int in a String field. How can I get passed this?

    Thank you
    Petra
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Tripmaker
    Hi! I'm new at Java and enjoying what I've learned to date. I'm busy designing a GUI (for customers of a Video store). My question is: I want to use Math.random() to generate a unique customer no. The problem is I'm using a TextField and the error that keeps coming up is that I'm unable to use int in a String field. How can I get passed this?

    Thank you
    Petra
    Hi and welcome to TSDN. I've moved your question to the Java forum.
    If you want an int value to be displayed as a String you can use ""+intValue (empty String concatenated with the int value).

    Have you gone through a Java tutorial yet?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      On top of all that Math.random() generates pseudo random numbers in the interval
      [0,1). Given the fact that a double has about 15 significant digits there is a slight
      chance that a random number will be generated that has been generated before.

      kind regards,

      Jos (<-- nitpicker)

      Comment

      • Tripmaker
        New Member
        • Jun 2007
        • 60

        #4
        Originally posted by r035198x
        Hi and welcome to TSDN. I've moved your question to the Java forum.
        If you want an int value to be displayed as a String you can use ""+intValue (empty String concatenated with the int value).

        Have you gone through a Java tutorial yet?

        Thanks for your welcome and help. I've just completed a 10 month intense Java course at a private college and am working on a project that needs to be handed in in 2 weeks time. So I'm very new at all of this. I've tried surging the tutorials but haven't anything to help me.

        Regards
        Petra

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Tripmaker
          Thanks for your welcome and help. I've just completed a 10 month intense Java course at a private college and am working on a project that needs to be handed in in 2 weeks time. So I'm very new at all of this. I've tried surging the tutorials but haven't anything to help me.

          Regards
          Petra
          You may also find the java.util.Rando m class to be of interest.

          Comment

          • blazedaces
            Contributor
            • May 2007
            • 284

            #6
            Originally posted by Tripmaker
            Hi! I'm new at Java and enjoying what I've learned to date. I'm busy designing a GUI (for customers of a Video store). My question is: I want to use Math.random() to generate a unique customer no. The problem is I'm using a TextField and the error that keeps coming up is that I'm unable to use int in a String field. How can I get passed this?

            Thank you
            Petra
            Try isolating your problem. For example, questions that pop up in my head:

            Doesn't math.random() produce a double between 0 and 1? Are you casting it to int? What are you doing along the way?

            Mostly, does the problem have anything to do with random()? Or is the problem simply that you can't put an int on a text field?

            Ways you can isolate your problem:

            -debug your code (i.e. print out values along the way until you discover EXACTLY where the problem lies, like if you're producing the correct number and it's simply not appearing or if the number is wrong as well, etc.)

            -test your methods out separately in a test-program

            Knowing what problem you actually have is the first step to finding a proper solution.

            Good luck and I hope this was helpful,

            -blazed

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              If I were the OP I'd simply marshal sequential numbers to the customers: 1, 2, 3
              etc. etc. The counter needs to be persistent but that's all. Use an eight byte long
              for the counter so you can hand out a unique number to every living creature in
              the solar system if you want; and that includes red cabbage, which I hate. ;-)

              kind regards,

              Jos

              Comment

              • Tripmaker
                New Member
                • Jun 2007
                • 60

                #8
                Originally posted by blazedaces
                Try isolating your problem. For example, questions that pop up in my head:

                Doesn't math.random() produce a double between 0 and 1? Are you casting it to int? What are you doing along the way?

                Mostly, does the problem have anything to do with random()? Or is the problem simply that you can't put an int on a text field?

                Ways you can isolate your problem:

                -debug your code (i.e. print out values along the way until you discover EXACTLY where the problem lies, like if you're producing the correct number and it's simply not appearing or if the number is wrong as well, etc.)

                -test your methods out separately in a test-program

                Knowing what problem you actually have is the first step to finding a proper solution.

                Good luck and I hope this was helpful,

                -blazed


                I can not put an int on a text field.

                this is part of my code:

                int randNo = 0;
                randNo = (int)((Math.ran dom()*700)+1);
                custIdText.setT ext(randNo);

                Petra

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by Tripmaker
                  I can not put an int on a text field.

                  this is part of my code:

                  int randNo = 0;
                  randNo = (int)((Math.ran dom()*700)+1);
                  custIdText.setT ext(randNo);

                  Petra
                  That problem was solved long ago in your first reply (see above) given by r035198x.

                  kind regards,

                  Jos

                  Comment

                  • Tripmaker
                    New Member
                    • Jun 2007
                    • 60

                    #10
                    Originally posted by JosAH
                    That problem was solved long ago in your first reply (see above) given by r035198x.

                    kind regards,

                    Jos


                    Hi Jos

                    Sorry, my fault. Didn't read the answer correctly. Too many late nights I guess.

                    Thanks for all you help

                    Petra

                    Comment

                    • RedSon
                      Recognized Expert Expert
                      • Jan 2007
                      • 4980

                      #11
                      I'm still worried about your design decision to use random numbers as unique customer numbers. I think its a poor decision and will likely cause errors a long time in the future probably long after you have moved on. If you don't want to use sequential numbering why not use the current system time to create a customer ID. Then in your documentation you can state that customer IDs are created by looking at the system time so be sure that your computer's time is always correct.

                      Comment

                      • blazedaces
                        Contributor
                        • May 2007
                        • 284

                        #12
                        Originally posted by RedSon
                        I'm still worried about your design decision to use random numbers as unique customer numbers. I think its a poor decision and will likely cause errors a long time in the future probably long after you have moved on. If you don't want to use sequential numbering why not use the current system time to create a customer ID. Then in your documentation you can state that customer IDs are created by looking at the system time so be sure that your computer's time is always correct.
                        Hey may not want it to be sequential or based on time so a customer can't "fake" a customer ID.

                        I think he should store somewhere the customer ID's already used. That way, even using random numbers he can always check if it's used already and if so, create a new random number. I'm not an expert, so there might be obvious reasons why this is also bad, but the logic makes sense in my head...

                        -blazed

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by blazedaces
                          Hey may not want it to be sequential or based on time so a customer can't "fake" a customer ID.

                          I think he should store somewhere the customer ID's already used. That way, even using random numbers he can always check if it's used already and if so, create a new random number. I'm not an expert, so there might be obvious reasons why this is also bad, but the logic makes sense in my head...

                          -blazed
                          If you generate numbers in sequence: 1, 2, 3 ... n you can also track that the
                          numbers in the interval [1,n] are already used and in a much cheaper way too.

                          kind regards,

                          Jos

                          Comment

                          • blazedaces
                            Contributor
                            • May 2007
                            • 284

                            #14
                            Originally posted by JosAH
                            If you generate numbers in sequence: 1, 2, 3 ... n you can also track that the
                            numbers in the interval [1,n] are already used and in a much cheaper way too.

                            kind regards,

                            Jos
                            Yes, but what if we treat the customer number sort of like the number on an install disc? If the computer asks for your customer ID and you input 1, you'll be fine no matter what... (I realize there could be other questions to cross-examine and make sure, but still).

                            I mean, even then cd key generators are written to get around this, but you have to admit it's a bit more secure...

                            Cheaper isn't necessarily better. Still, I have no idea what the goal of this program is so I'm just throwing things in the air here...

                            -blazed

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by RedSon
                              I'm still worried about your design decision to use random numbers as unique customer numbers. I think its a poor decision and will likely cause errors a long time in the future probably long after you have moved on. If you don't want to use sequential numbering why not use the current system time to create a customer ID. Then in your documentation you can state that customer IDs are created by looking at the system time so be sure that your computer's time is always correct.
                              Yep, this should really be done database side if the OP is using a database.

                              Comment

                              Working...