PDA

View Full Version : عدد تصادفی



Boo Ali
پنج شنبه 29 فروردین 1387, 13:33 عصر
با سلام

می خواهم بین دو عدد دلخواه یک عدد تصادفی تولید شود. تابع Rnd فقط بین 0 و 1 تولید میکند.

raravaice
پنج شنبه 29 فروردین 1387, 13:38 عصر
Dim a as new Random
Dim b as integer
b=a.next(100,1000)



موفق باشید

علیرضا مداح
پنج شنبه 29 فروردین 1387, 14:55 عصر
سلام ،
با استفاده از تابع Rnd هم اینکار امکان پذیر است :


randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound))

کلاس Random :


Dim r As New Random()
r.Next(minValue, maxValue)

و بهترین روش استفاده از کلاس Security.Cryptography.RNGCryptoServiceProvider :
مثالی از MSDN :


'The following sample uses the Cryptography class to simulate the roll of a dice.
Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Class RNGCSP

' Main method.
Public Shared Sub Main()
' Roll the dice 30 times and display
' the results to the console.
Dim x As Integer
For x = 0 To 30
Console.WriteLine(RollDice(6))
Next x
End Sub 'Main

' This method simulates a roll of the dice. The input parameter is the
' number of sides of the dice.
Public Shared Function RollDice(NumSides As Integer) As Integer
' Create a byte array to hold the random value.
Dim randomNumber(0) As Byte

' Create a new instance of the RNGCryptoServiceProvider.
Dim Gen As New RNGCryptoServiceProvider()

' Fill the array with a random value.
Gen.GetBytes(randomNumber)

' Convert the byte to an integer value to make the modulus operation easier.
Dim rand As Integer = Convert.ToInt32(randomNumber(0))

' Return the random number mod the number
' of sides. The possible values are zero-
' based, so we add one.
Return rand Mod NumSides + 1
End Function 'RollDice
End Class 'CryptoMemoryStream