Hack Forums
[.NET] 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] generate a random number (/showthread.php?tid=5574041)



generate a random number - Sickness - Mar 13, 2017

Well need a easy code but I dont know what to do:
I need to generate a random number like this:
[Image: b0748d24e8ea2d05bdf2deb078ce7e5d.png]

What I can put there? 19 numbers


* - Visual Freedom - Mar 13, 2017

Just use Random().Next(1000,9999) and use concat to add them into a row of many


* - Barret - Mar 13, 2017

PHP Code

<?php 
Const Chartable As String = "0123456789"
Public Shared Function Randomize(len As Integer) As String
 
Static rnd As New Random(New Object().GetHashCode Mod DateTime.Now.Millisecond)
 Return New 
String(Chartable.Select(Function(x) Chartable(rnd.Next(Chartable.Length))).Take(len).ToArray())
End Function 



* - Sn0wX - Mar 13, 2017

ONE instance and one alone now mr barret :)


* - BoliBerrys - Mar 13, 2017

Code
const string numbers = "0123456789";
Random r = new Random();
string num = "";
for(int i = 0; i<=19;i++)
{
   num += numbers[r.Next(0, numbers.Length -1)];
}
Now you just add num to the URL or whatever you want.

EDIT:

I just realized this is the VB section and not the C#, so my code is useless to you.


* - Barret - Mar 13, 2017

(Mar 13, 2017 06:02 PM)BoliBerrys Wrote:  
Code
const string numbers = "0123456789";
Random r = new Random();
string num = "";
for(int i = 0; i<=19;i++)
{
   num += numbers[r.Next(0, numbers.Length -1)];
}
Now you just add num to the URL or whatever you want.

EDIT:

I just realized this is the VB section and not the C#, so my code is useless to you.

you lil fucker replicated my version :P hahahaha 🤣


* - BahNahNah - Mar 13, 2017

Heres how i would do it:
Code
Dim r As New Random()
Const charMap As String = "1234567890"

Function GetRandomNumberString(len As Integer) As String
    Dim sb As New StringBuilder(len)
    For i As Integer = 0 To len
        sb.Append(charMap(r.Next(charMap.Length)))
    Next
Return sb.ToString()
End Function

And heres a short verson for the lols
Code
Function GetRandomNumberString(len As Integer) As String
    Return String.Concat(Enumerable.Repeat(Function() charMap(r.Next(charMap.Length)), len))
End Function



* - BoliBerrys - Mar 14, 2017

(Mar 13, 2017 07:29 PM)Barret Wrote:  you lil fucker replicated my version :P hahahaha 🤣

No idea what u coded haha

Sorry if its basically the same 😭😭


* - Barret - Mar 14, 2017

(Mar 14, 2017 06:09 PM)BoliBerrys Wrote:  No idea what u coded haha

Sorry if its basically the same 😭😭


Dont sweat it, nothing is ever owned, everything is copied of something. Heck even nature does that. :P


* - Sn0wX - Mar 14, 2017

(Mar 14, 2017 07:25 PM)Barret Wrote:  Dont sweat it, nothing is ever owned, everything is copied of something. Heck even nature does that. :P

some one "your" level should know NEVER initialize the random class multiple times.


* - BahNahNah - Mar 14, 2017

(Mar 14, 2017 07:35 PM)Sn0wX Wrote:  some one "your" level should know NEVER initialize the random class multiple times.

Its static. There is only one instance.


* - Barret - Mar 15, 2017

(Mar 14, 2017 09:18 PM)BahNahNah Wrote:  Its static. There is only one instance.

Just ignore it, hes just being silly. Just look beyond and be fine.


* - Sn0wX - Mar 15, 2017

Urgh child's time.

Static cant use different seeds.
It's not thread safe.
This is basic GOOD CODING STANDARDS.

but hey when you guys use so many single instances in your apps i would expect you to carry THEM.

oh wait you do not.

You wouldn't be able to use different seeds if it were static - the Random instance keeps track of that state. By default Random uses the current time as seed


* - BahNahNah - Mar 15, 2017

(Mar 15, 2017 04:47 PM)Sn0wX Wrote:  Urgh child's time.

Static cant use different seeds.
It's not thread safe.
This is basic GOOD CODING STANDARDS.

but hey when you guys use so many single instances in your apps i would expect you to carry THEM.

oh wait you do not.

You wouldn't be able to use different seeds if it were static - the Random instance keeps track of that state. By default Random uses the current time as seed

What are you talking about? Objects are reference types, the sttaic var just stores the pointer to the random Instance.