![]() |
|
[.NET] How do I generate a random number? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [.NET] How do I generate a random number? (/showthread.php?tid=4477126) Pages: Pages
1
2
|
How do I generate a random number? - Man16 - Oct 5, 2014 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)Any simpler way to do it? Thanks * - +Haze - Oct 5, 2014 PHP Code Freestyled it but it should work. Basically it generates a random number ranging from 0 to 5 Easy. * - Only3 - Oct 5, 2014 MsgBox(Guid.NewGuid.ToString.Where(Function(n) Char.IsDigit(n)).ToArray) * - IAmNotHere - Oct 5, 2014 Code Dim o As Integer* - TheBarret - Oct 5, 2014 (Oct 5, 2014 02:02 PM)+Haze Wrote: Replace the Dim with "static" so the instance will only be initiated once the entire session of the application being runned. * - Zetonix - Oct 6, 2014 Hope this thread will help you:- https://hackforums.net/showthread.php?tid=4477974 Thanks, * - satanrulze - Oct 6, 2014 Sometimes googling something goes a long way.Try it once in a while. :) * - IAmNotHere - Oct 6, 2014 (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. * - Only3 - Oct 6, 2014 (Oct 6, 2014 02:51 PM)IAmNotHere Wrote: Hello TheBarret, http://msdn.microsoft.com/en-us/library/z2cty7t8.aspx * - Cairns - Oct 6, 2014 (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) * - IAmNotHere - Oct 6, 2014 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. * - TheBarret - Oct 6, 2014 (Oct 6, 2014 02:51 PM)IAmNotHere Wrote: Hello TheBarret, 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. * - IAmNotHere - Oct 6, 2014 Quote:Well its quite easy, the static , name already gives you a clue what it does,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 Form1Quote: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 numbersI 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. * - Eliza - Oct 6, 2014 http://www.random.org/ will generate a random number for you efficiently. * - TheBarret - Oct 6, 2014 (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. |