+ Reply to Thread
Results 1 to 4 of 4

Thread: Getting Random numbers in VB.NET

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Getting Random numbers in VB.NET

    I will in this tutorial show you how you could get random numbers using VB.NET. Before we start I have to tell you that it's impossible to get a completely random number with a computer since they're all following different calculation. But the random numbers you can get is in most cases "random enough". The random number generator we'll use has this description:

    Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet certain statistical requirements for randomness.
    So the numbers you'll get will be "random enough" even though they aren't actually random. Never mind, let's start.



    To create a random generator we just uses the data type Random, like so:

    [highlight=VB.NET] Dim r As New Random[/highlight]

    To then get a random positive integer number within the span of an integer value in VB.NET use:

    [highlight=VB.NET]r.Next()[/highlight]

    But you have to be careful if you want to generate more then one random number, if you do like this all numbers will be the same:

    [highlight=VB.NET] For i As Integer = 0 To 99
    Dim r As New Random
    r.Next()
    Next[/highlight]

    To make them all different you have to do this:

    [highlight=VB.NET] Dim r As New Random
    For i As Integer = 0 To 99
    r.Next()
    Next[/highlight]


    Even though we're doing this the 100 numbers will be "random" but if we call the function(or where we now have this code) again, the numbers won't be the same as before but they could look a bit like them(all depending on how often and when we calls the function(or whatever it is) again, but they could look very much the same). So therefor I recommend you to declare it as "unlocal" as you can to get the "most random" result, like this:


    [highlight=VB.NET]Public Class frmMain

    Dim r As New Random
    Private Sub mySub()

    For i As Integer = 0 To 99
    r.Next()
    Next

    End Sub
    End Class[/highlight]


    An example on this could be an old code I made which took a lot of puzzle pieces and randomized there positions, It added one piece at a random interval too but when I randomized its position I declared a new Random generator each time which resulted in this (It doesn't look so random, right? ):


    Getting Random numbers in VB.NET-random.jpg





    You can also set a exclusive maximal value the Random Generator can generate, like this:

    [highlight=VB.NET] r.Next(11)[/highlight]

    Or both the inclusive minimal value and the exclusive maximal:

    [highlight=VB.NET] r.Next(5, 11)[/highlight]

    Or if you want to get a Double value instead on an Integer you can use NextDouble(), then you'll receive a "random" number between 0.0 and 1.0, you can't change the minimal value and maximal value when using NextDouble. So NextDouble simply looks like this:

    [highlight=VB.NET] r.NextDouble()[/highlight]


    This was it, remember that none of the random numbers you'll get will be completely random no matter how many times you're using the number generators to get only one number, but they are very often "random enough". Hope you enjoyed this tutorial

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Getting Random numbers in VB.NET

    Very cool! +rep

  4. #3
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: Getting Random numbers in VB.NET

    Quote Originally Posted by Jordan View Post
    Very cool! +rep
    Thanks But you've forgot the +rep

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Getting Random numbers in VB.NET

    Useful +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sort random numbers...
    By Surfensteve in forum Visual Basic Programming
    Replies: 3
    Last Post: 03-08-2011, 11:49 PM
  2. NASM Generating random numbers
    By untitled_1 in forum Assembly
    Replies: 2
    Last Post: 10-30-2010, 10:24 AM
  3. How do you generate random numbers?
    By leroy in forum Visual Basic Programming
    Replies: 23
    Last Post: 09-04-2009, 09:46 AM
  4. An array of random numbers
    By ld_pvl in forum Java Help
    Replies: 3
    Last Post: 02-11-2009, 02:56 PM
  5. Random Numbers!
    By PascalPro in forum Visual Basic Programming
    Replies: 4
    Last Post: 08-08-2008, 02:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

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