Results 1 to 6 of 6

Thread: VB.net VS2022 => How to generate random number that is a Single or Double

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2024
    Posts
    18

    VB.net VS2022 => How to generate random number that is a Single or Double

    Trying to generate a Single or Double random number between 0 and 1 inclusive of 0 and 1.

    I can use rnd() method & it works but everyone on the various VB fora claim this is a bad way to do it (old & pseudorandom length too short)

    I have tried "RandomNumberGenerator.GetInt32" and apparently this is a better method but seems to only generate integer values. I need Single/Double capability.
    I do not need Crypto (just doing science Monte Carlo calculation) but would love a really long sequence before repeats.

    Thanks for any help guys!

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: VB.net VS2022 => How to generate random number that is a Single or Double

    You can just use the Random class to do this:-
    Code:
            Dim r As New Random
    
            'Generates a random number between 0 and 1
            Dim number As Double = r.NextDouble()
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2024
    Posts
    18

    Re: VB.net VS2022 => How to generate random number that is a Single or Double

    Thank you Niya, I will try this! Looks simple enough. Thank you.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: VB.net VS2022 => How to generate random number that is a Single or Double

    Note that the upper range is less than 1 so the largest number generated will be something like 0.99...
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.net VS2022 => How to generate random number that is a Single or Double

    Note that both RandomNumberGenerator and Random can generate random Bytes, so you can use those Bytes any way you want, e.g. use BitConverter to convert four Bytes to a Single. That said, you'd probably just use Random.NextDouble and Convert.ToSingle, if a Single is what you need.

    Note also that the Random class has had a Shared property since .NET 6, so you don't need to create your own instance anymore, unless you need multiple different instances or to specify your own seed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2024
    Posts
    18

    Re: VB.net VS2022 => How to generate random number that is a Single or Double

    Great input jmcilhinney... this will help get exactly what I need. Appreciate the help from you both!!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width