Jump to content




Recent Status Updates

  • Photo
      30 Sep
    rhossis

    laptop hard disk seated beneath motherboard but with no access panel. 7 hours to replace :(

    Show comments (3)
  • Photo
      19 Sep
    Chall

    I love it when you go to write a help thread, then while writing, you reach an enlightenment, and figure it out yourself.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Getting Random numbers in VB.NET

VB.NET random number

  • Please log in to reply
4 replies to this topic

#1 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 08 November 2009 - 07:41 AM

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:

        Dim r As New Random


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

r.Next()


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:

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


To make them all different you have to do this:

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



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:


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



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? :)):


[ATTACH]2315[/ATTACH]





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

            r.Next(11)


Or both the inclusive minimal value and the exclusive maximal:

            r.Next(5, 11)


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:

            r.NextDouble()



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 :)

Attached Thumbnails

  • Random.jpg

  • 1

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 08 November 2009 - 03:19 PM

Very cool! +rep
  • 0

#3 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1,989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 08 November 2009 - 11:10 PM

Very cool! +rep


Thanks :) But you've forgot the +rep :D
  • 0

#4 WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderator
  • 17,133 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 November 2009 - 08:15 AM

Useful :) +rep
  • 0

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

My MineCraft server site: http://banishedwings.enjin.com/


#5 Reza1

Reza1

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Programming Language:C++, Visual Basic .NET, Assembly, Others
  • Learning:C#, JavaScript, Ruby

Posted 04 January 2013 - 06:15 PM

Hi every one I need an idea to generate a random value without a specific number between ,I mean assume that minimum is min and maximum is max and you have a number x between: min........x.......max
i want a random number between min and max and without being x. I thought of many ideas but because it's repeating for many times in my program i need the simplest idea for this: i thought of -if- but because this is in a case select which is in a for loop which it's in a if which it's in another loop and i'm going to right it for 20 cases , i'm afraid it must not be so complex. please help me , if you have ideas,any idea. :lol:
  • 0





Also tagged with one or more of these keywords: VB.NET, random number