Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

How do I generate a random number?

I've tried looking for a method to generate a random number for me in VB.Net and it does not look simple like in other languages such as python. Currently, I have this:

Code
Function random(ByVal maxNumber As Integer)
        Dim temp = New Random()

        'Return CInt(Math.Ceiling(Rnd() * maxNumber))
       Return temp.Next(maxNumber)
    End Function

Any simpler way to do it?

Thanks
PHP Code

<?php 
Dim R 
As New Random
R
.Next(0,5) 

Freestyled it but it should work. Basically it generates a random number ranging from 0 to 5
Easy.
MsgBox(Guid.NewGuid.ToString.Where(Function(n) Char.IsDigit(n)).ToArray)
Code
Dim o As Integer
Dim r As New Random()
Dim m As Integer = 32
For m  = 1 To m
o = r.Next(m)
Next
Replace the m value with the desired maximum and o will hold the result.
(Oct 5, 2014 02:02 PM)+Haze Wrote:  
PHP Code

<?php 
Dim R 
As New Random
R
.Next(0,5) 

Freestyled it but it should work. Basically it generates a random number ranging from 0 to 5
Easy.

Replace the Dim with "static" so the instance will only be initiated once the entire session of the application being runned.
Hope this thread will help you:-

https://hackforums.net/showthread.php?tid=4477974

Thanks,
Sometimes googling something goes a long way.Try it once in a while. :)
(Oct 5, 2014 03:50 PM)TheBarret Wrote:  Replace the Dim with "static" so the instance will only be initiated once the entire session of the application being runned.
Hello TheBarret,
Can you, maybe recommend a reading about what you told here? If I understand you I can add more speed an accuracy in my applications. I want to rewrite a few applications I made to see, if they perform better when I add, I think better code like the code you typed.
(Oct 6, 2014 02:51 PM)IAmNotHere Wrote:  Hello TheBarret,
Can you, maybe recommend a reading about what you told here?

http://msdn.microsoft.com/en-us/library/z2cty7t8.aspx
(Oct 5, 2014 03:50 PM)TheBarret Wrote:  Replace the Dim with "static" so the instance will only be initiated once the entire session of the application being runned.

Bet me to it, lol.

The guid is a good method also but I prefer this one (a static instance - much easier)
You declare a non shared "Static" for a class and you declare a shared "Static" for the whole application, which can be called by using the class name first?

-Non shared "Static" for 1 class only value.
-But a shared "Static", after a call from all classes changes it's value and the new value is taken to every new call from whichever class?

Regards.
(Oct 6, 2014 02:51 PM)IAmNotHere Wrote:  Hello TheBarret,
Can you, maybe recommend a reading about what you told here? If I understand you I can add more speed an accuracy in my applications. I want to rewrite a few applications I made to see, if they perform better when I add, I think better code like the code you typed.

Well its quite easy, the static , name already gives you a clue what it does,
the instance will be loaded once and keep its object, so in a shared or private method with the static Random object that is declared,
will not be re-declared or re-initated on a second or more call.
Normaly an object in a method will be released after it returns or ends, static will retain that object in place.

With the random class you notice that if you do not use the static you can recognize that the random.next gives sometimes a repeated pattern of random numbers, now you can change that by giving it a seed, i usually put in the miliseconds from Now.miliseconds(), combine that with static, that will not be case coz it will not be removed or released andthus will keep giving randoms from the old instance.

msdn details:
Quote:Normally, a local variable in a procedure ceases to exist as soon as the procedure stops. A static variable continues to exist and retains its most recent value. The next time your code calls the procedure, the variable is not reinitialized, and it still holds the latest value that you assigned to it. A static variable continues to exist for the lifetime of the class or module that it is defined in.
Quote:Well its quite easy, the static , name already gives you a clue what it does,
the instance will be loaded once and keep its object, so in a shared or private method with the static Random object that is declared,
will not be re-declared or re-initated on a second or more call.
Normaly an object in a method will be released after it returns or ends, static will retain that object in place.
Isn't this a bit like dimming a variable under the Public Class Form1 line in the form1.vb also?
I have codes somehow like this:
Code
Public Class Form1
    Inherits Form
    Dim count As Integer = -1
      Dim strLine As String

    <STAThread()> _
    Public Shared Sub Main()
      Application.EnableVisualStyles()
      Application.Run(New Form1())
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    End Sub

Quote:With the random class you notice that if you do not use the static you can recognize that the random.next gives sometimes a repeated pattern of random numbers
I has this in the past and for the same randoms I placed every time a new function at the end. The "Static's" will save me a lot off coding time.
http://www.random.org/ will generate a random number for you efficiently.
(Oct 6, 2014 03:25 PM)Eliza Wrote:  http://www.random.org/ will generate a random number for you efficiently.

Its not a bad idea, make an API for that and you got yourself a nigty randomizer.

But in speed wise, its not gonna be faster then the random class in .net framework ofc
True random, its not rly all true random, but we are satisfied by a set of numbers that will change in a way that we can call random ^^

that or keep 10 asians in ur basement and let them memorize some random numbers and yell them if u need it.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by MaskinApr 21, 2021 01:52 AM 5 322 Apr 21, 2021 01:52 AM
Last Post: Vector Dynamics
[C] by Tr0jandogJul 18, 2020 12:05 AM 4 224 Jul 18, 2020 12:05 AM
Last Post: Xora
[C#] by draxyeeDec 28, 2019 03:44 PM 2 292 Dec 28, 2019 03:44 PM
Last Post: Eternal Darkness