|
9692
|
Programación / .NET (C#, VB.NET, ASP) / Re: problema con Random
|
en: 18 Enero 2013, 17:41 pm
|
Poca funcionalidad más puede tener un "get random numbers", sólamente elegir si se quiere un número décimal o... bueno, ya vendrá, primero necesito que funcione xD
Lo de la lista solo era un ejemplo de uno de los casos en los que siempre manda el mismo número. 1- NO generar la instancia de clase Random, ni la semilla, dentro de un bucle. Ya que si lo haces, puedes tener la seguridad de que los valores se repitan.
2- NO generar la semilla ni la instancia del objeto Random dentro de un método que será llamado de forma consecutiva. Podías pensar, que si en lugar de crear la instancia dentro del bucle, la creas fuera se soluciona, pero si ese "fuera" es dentro del método, la verdad es que no. Pues vaya .
Private Sub GetRandomNumbers(Byval Min as int32, Byval Max as int32) ' Inicializamos el generador de nº aleatorios Randomize() ' Generamos un valor aleatorio entre 50 y 100 Dim Valor As Integer = CInt(Int((100 * Max()) + Min)) return Valor End Sub
Se nota que lo has escrito al vuelo, no funciona! xD He intentado corregirlo pero me devuelve siempre valores por encima de "400"
|
|
|
9693
|
Programación / .NET (C#, VB.NET, ASP) / [SOLUCIONADO] problema con Random
|
en: 18 Enero 2013, 17:18 pm
|
¿Que debo hacer para que esto funcione? Si declaro la variable "Rando" fuera de la función, funciona, pero precísamente lo que pretendo es ahorrarme la necesidad de declarar la variable fuera de la función... xD Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim a As New List(Of String) For x As Integer = 0 To 10 : a.Add(Get_Random_Number(1, 100)) : Next For Each f In a : MsgBox(f) : Next ' El número siempre es el mismo End Sub Public Function Get_Random_Number(ByVal min As Integer, ByVal max As Integer) As Integer Dim Rando As New Random Return Rando.Next(min, max) End Function End Class
|
|
|
9694
|
Programación / .NET (C#, VB.NET, ASP) / Re: Bucles
|
en: 18 Enero 2013, 16:42 pm
|
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Size = New Point(600, 600) Dim Rando As New Random ' Creo los controles y los añado al form, para este ejemplo. Dim Button_(10) As Button For num = 0 To 10 : Button_(num) = New Button : Me.Controls.Add(Button_(num)) : Next For Each Control In Me.Controls If Control.GetType.ToString.Contains("Button") Then ' Si el control es de tipo Button... Control.location = New Point(Rando.Next(1, 500), Rando.Next(1, 500)) ' Los coloco aleatóriamente End If Next End Sub End Class
|
|
|
9696
|
Programación / .NET (C#, VB.NET, ASP) / Re: [APORTE] Snippets !! (Posteen aquí sus snippets)
|
en: 18 Enero 2013, 14:06 pm
|
No es mucho, pero puede servir... Obtener la ruta del directorio o del archivo "user.config" #Region " Get User Config Function " ' [ Get User Config Function ] ' ' // By Elektro H@cker (Gracias a Seba123Neo) ' ' Examples : ' ' * First add a reference to "System.Configuration" in the proyect ' ' MsgBox(Get_User_Config(User_Config.File)) ' MsgBox(Get_User_Config(User_Config.Path)) Enum User_Config Path End Enum Private Function Get_User_Config(ByVal Setting As User_Config) As String Dim UserConfig As String = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoaming).FilePath Select Case Setting Case User_Config. File : Return UserConfig Case User_Config.Path : Return UserConfig.Substring(0, UserConfig.LastIndexOf("\")) Case Else : Return False End Select End Function #End Region
|
|
|
9698
|
Programación / .NET (C#, VB.NET, ASP) / Re: Una pregunta sobre IO.Directory.GetFiles
|
en: 18 Enero 2013, 13:17 pm
|
Puf, para perder tiempo de lectura de disco haciendo múltiples búsquedas y/o filtrando la lista... busco algo más "natural" creo que jamás diría esto, pero es que para eso es mejor usar el comando "DIR" de la CMD (en VB.NET) sincéramente xD. @3mp3z@ndo Gracias por la info , parece la mejor opción pero me queda la duda, ¿Esto solo hace una búsqueda y filtra, o hace dos búsquedas? Dim files = Directory. GetFiles("C:\", "*"). Where(Function(s ) s. EndsWith(".exe") OrElse s. EndsWith(".xml"))
|
|
|
9699
|
Programación / .NET (C#, VB.NET, ASP) / Re: [APORTE] Snippets !! (Posteen aquí sus snippets)
|
en: 18 Enero 2013, 11:27 am
|
Obtener la arquitectura del OS #Region " Get OS Architecture Function " ' [ Get OS Architecture Function ] ' ' // By Elektro H@cker ' ' Examples : ' Dim Architecture = Get_OS_Architecture() Private Function Get_OS_Architecture() As Integer Dim Bits = Runtime.InteropServices.Marshal.SizeOf(GetType(IntPtr)) * 8 Select Case Bits Case 32 : Return 32 ' x86 Case 64 : Return 64 ' x64 Case Else : Return Nothing ' xD End Select End Function #End Region
Ejemplo de un overload ' Examples: ' ' Test(0) ' Test"0") Sub Test(ByVal Argument As Integer) MsgBox("Integer: " & Argument) End Sub Sub Test(ByVal Argument As String) MsgBox("String: " & Argument) End Sub
El snippet de Get All Files, mejorado: #Region " Get All Files Function " ' [ Get All Files Function ] ' ' // By Elektro H@cker ' ' Examples: ' ' Dim Files As Array = Get_All_Files("C:\Test", True) ' For Each File In Get_All_Files("C:\Test", False) : MsgBox(File) : Next Private Function Get_All_Files(ByVal Directory As String, Optional ByVal Recursive As Boolean = False) As Array If System.IO.Directory.Exists(Directory) Then If Not Recursive Then : Return System.IO.Directory.GetFiles(Directory, "*", IO.SearchOption.TopDirectoryOnly) Else : Return IO.Directory.GetFiles(Directory, "*", IO.SearchOption.AllDirectories) End If Else Return Nothing End If End Function #End Region
|
|
|
9700
|
Programación / .NET (C#, VB.NET, ASP) / [SOLUCIONADO] Una pregunta sobre IO.Directory.GetFiles
|
en: 18 Enero 2013, 10:10 am
|
¿No se puede usar algún separador de expresión? He intentado esto: IO.Directory.GetFiles(Directory, "*.exe, *.vb") IO.Directory.GetFiles(Directory, "*.exe; *.vb") IO.Directory.GetFiles(Directory, "*.exe" + "*.vb") IO.Directory.GetFiles(Directory, "*.exe" + IO.Path.AltDirectorySeparatorChar + "*.vb") IO.Directory.GetFiles(Directory, "*.exe" + IO.Path.DirectorySeparatorChar + "*.vb")
|
|
|
|
|
|
|