Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Obtaining Random Numbers

Obtaining Random Numbers

Scheduled Pinned Locked Moved Visual Basic
lounge
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WestSideRailways
    wrote on last edited by
    #1

    Hi there, I have done a search for generating random numbers and have come up empty, as anything i have read does not make any sense to me.:sigh::confused: Could someone please show me how i could do this. What i would like to do is get a random number between to numbers, sound simple does it not.:)

    C CPalliniC 2 Replies Last reply
    0
    • W WestSideRailways

      Hi there, I have done a search for generating random numbers and have come up empty, as anything i have read does not make any sense to me.:sigh::confused: Could someone please show me how i could do this. What i would like to do is get a random number between to numbers, sound simple does it not.:)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      It sure is simple. Are you using VB.NET ? If so, Random.Next is all you need.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      CPalliniC 1 Reply Last reply
      0
      • W WestSideRailways

        Hi there, I have done a search for generating random numbers and have come up empty, as anything i have read does not make any sense to me.:sigh::confused: Could someone please show me how i could do this. What i would like to do is get a random number between to numbers, sound simple does it not.:)

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        MSDN is your best friend, for instance:

        To produce random integers in a given range, use the following formula.
        (Visual Basic)

        randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))

        if you use .NET then you have to deal with Random class, again, MSDN will help, see here. To obtain more detailed help, please, clearify your needs (e.g. Do you need integers or double numbers?) and tell me the programming language you intend to use. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        In testa che avete, signor di Ceprano?

        W 1 Reply Last reply
        0
        • C Christian Graus

          It sure is simple. Are you using VB.NET ? If so, Random.Next is all you need.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Christian Graus wrote:

          It sure is simple. Are you using VB.NET ?

          In this forum, you have to ask the above question quite often. It's boring..., VB6 and VB.NET are completely different topics. Sorry, I want only to share a bit my frustration... :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • CPalliniC CPallini

            MSDN is your best friend, for instance:

            To produce random integers in a given range, use the following formula.
            (Visual Basic)

            randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))

            if you use .NET then you have to deal with Random class, again, MSDN will help, see here. To obtain more detailed help, please, clearify your needs (e.g. Do you need integers or double numbers?) and tell me the programming language you intend to use. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            W Offline
            W Offline
            WestSideRailways
            wrote on last edited by
            #5

            Thank you for your replys.:):cool: This is what i had and was not doing the job(kept on going out of bounds,which cause an error) RandomValue = CInt(Int(upper) * Rnd() + lower) your code fixed that problem in the test app. Will try in in my app.Just to make sure it works;) oh, BTW i forgot to say which language i was using:doh: thought that you would have been a mind reader:rolleyes::laugh::laugh::laugh: I am using VB2005. I am back, thought i used the same post.... I changed your line to the following :- RandomValue = CInt(Int(upper - lower + 0) * Rnd() + lower) works perfectly now.:-D:cool: Thank again for all your replys. -- modified at 5:36 Friday 5th January, 2007

            C 1 Reply Last reply
            0
            • W WestSideRailways

              Thank you for your replys.:):cool: This is what i had and was not doing the job(kept on going out of bounds,which cause an error) RandomValue = CInt(Int(upper) * Rnd() + lower) your code fixed that problem in the test app. Will try in in my app.Just to make sure it works;) oh, BTW i forgot to say which language i was using:doh: thought that you would have been a mind reader:rolleyes::laugh::laugh::laugh: I am using VB2005. I am back, thought i used the same post.... I changed your line to the following :- RandomValue = CInt(Int(upper - lower + 0) * Rnd() + lower) works perfectly now.:-D:cool: Thank again for all your replys. -- modified at 5:36 Friday 5th January, 2007

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              If you are using VB2005, then dont use the solution provided, as it is for VB6. Use VB.NET, which means Random.Next

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              W 1 Reply Last reply
              0
              • C Christian Graus

                If you are using VB2005, then dont use the solution provided, as it is for VB6. Use VB.NET, which means Random.Next

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                W Offline
                W Offline
                WestSideRailways
                wrote on last edited by
                #7

                Wel:confused:l my system will not recognize the command .next with the Random command????

                D 1 Reply Last reply
                0
                • W WestSideRailways

                  Wel:confused:l my system will not recognize the command .next with the Random command????

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Public Class Form1
                  .
                  .
                  Private RNG As New Random()
                  .
                  .
                  .
                  Dim newInteger As Integer = RNG.Next(0, 1000)
                  .
                  .
                  .

                  Seriously, pick up a beginners book on VB.NET. This is an easy one that gave you a ton of trouble. Learn the basics before you frustrate yourself out of doing this.

                  Dave Kreskowiak Microsoft MVP - Visual Basic

                  W 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Public Class Form1
                    .
                    .
                    Private RNG As New Random()
                    .
                    .
                    .
                    Dim newInteger As Integer = RNG.Next(0, 1000)
                    .
                    .
                    .

                    Seriously, pick up a beginners book on VB.NET. This is an easy one that gave you a ton of trouble. Learn the basics before you frustrate yourself out of doing this.

                    Dave Kreskowiak Microsoft MVP - Visual Basic

                    W Offline
                    W Offline
                    WestSideRailways
                    wrote on last edited by
                    #9

                    Everything is EASY, once you have done it.... My Friend;P and i have a book but it does not say anything about random numbers..... Mastering VB2005 EE As allways, Thank You for your input.:-D

                    C D 2 Replies Last reply
                    0
                    • W WestSideRailways

                      Everything is EASY, once you have done it.... My Friend;P and i have a book but it does not say anything about random numbers..... Mastering VB2005 EE As allways, Thank You for your input.:-D

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      WestSideRailways wrote:

                      Everything is EASY, once you have done it....

                      That's true. But, if you couldn't follow the MSDN help, then the problem is not a book on VB, it's knowing enough VB to be able to navigate sources of new information. I second what Dave said, you need to learn some basics, so that you're able to use information sources. I expected you to realise I was telling you that there's a Next method on the random class, and to be able to work out that you need to create an instance of the Random class.

                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                      W 1 Reply Last reply
                      0
                      • W WestSideRailways

                        Everything is EASY, once you have done it.... My Friend;P and i have a book but it does not say anything about random numbers..... Mastering VB2005 EE As allways, Thank You for your input.:-D

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #11

                        It wasn't the topic of random numbers that was giving you the problem. The Random class is just like any other. Instantiate the class, which is then called an "object", then call the methods or set properties on it. The entire .NET Framework works, at a minimum, just like this. There is, of course, far more to the rest of the Framework, but once you understand the basics of how to instantiate a simple class, like Random, and use it, you can pretty much find your way through the MSDN documentation and figure out how more complex classes work.

                        Dave Kreskowiak Microsoft MVP - Visual Basic

                        1 Reply Last reply
                        0
                        • C Christian Graus

                          WestSideRailways wrote:

                          Everything is EASY, once you have done it....

                          That's true. But, if you couldn't follow the MSDN help, then the problem is not a book on VB, it's knowing enough VB to be able to navigate sources of new information. I second what Dave said, you need to learn some basics, so that you're able to use information sources. I expected you to realise I was telling you that there's a Next method on the random class, and to be able to work out that you need to create an instance of the Random class.

                          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                          W Offline
                          W Offline
                          WestSideRailways
                          wrote on last edited by
                          #12

                          The idea of being a good gru , is knowing that not every body it at the same level as yourself. and sometimes you might have to come down off the high horse and help the people , by explaining what you have said in more detail. Don't expect some people to know what you are talking about , just because you do. I know it a bad habbit that we all do to other people at work, myself included. Hopefully you don't take this the wrong way.Was not meant to be taken the wrong way, I am just giving my opion on the matter. Again hopefully when i ask another question you might remember this and answer in a more explain way.:):):):)

                          C 1 Reply Last reply
                          0
                          • W WestSideRailways

                            The idea of being a good gru , is knowing that not every body it at the same level as yourself. and sometimes you might have to come down off the high horse and help the people , by explaining what you have said in more detail. Don't expect some people to know what you are talking about , just because you do. I know it a bad habbit that we all do to other people at work, myself included. Hopefully you don't take this the wrong way.Was not meant to be taken the wrong way, I am just giving my opion on the matter. Again hopefully when i ask another question you might remember this and answer in a more explain way.:):):):)

                            C Offline
                            C Offline
                            Christian Graus
                            wrote on last edited by
                            #13

                            Well, had Dave not stepped in, I would have answered in more detail.  The truth is, I give a first answer that means that anyone with access to google has all they need.  Anyone who proves incapable of using google, or perhaps, understanding what MSDN says, I will give deeper help.  But, spoon feeding people on every simple question, that can be answered by google in seconds, is not helping them.  I'm trying to teach people how to search for information themselves, when the question is trivial. And that's what someone who wants to help *should* be doing.  No matter what your opinion, if you ask more, I will answer again.  I don't keep track of what people say in the past, or keep a list of people to help, or not help.

                            Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups