ramil
01-23-2008, 08:34 PM
hello, im now creating a program that need's to randomize 4 numbers and stored it's value into only one variable should return 4 values can you hep me?plese?.
random numbersramil 01-23-2008, 08:34 PM hello, im now creating a program that need's to randomize 4 numbers and stored it's value into only one variable should return 4 values can you hep me?plese?. wbeard52 01-23-2008, 08:42 PM we are going to need more information, how is it that you want to store four numbers in a single variable and then retrieve them separately? Are you talking about an array? JPB 01-23-2008, 08:44 PM What are the ranges of the numbers that you need to generate? Do you need to be able to separate the numbers once they are returned, or do you need to concatenate them into a single number? MSDN mentions this code to generate random numbers within a specified range: Int((upperbound - lowerbound + 1) * Rnd + lowerbound) You can easily generate four random numbers in a For..Next loop. Within that loop you could assign the numbers to an Array (4 elements but 1 return value for your function), or you could concatenate them into a string for a single return value where separation of individually generated numbers is not required. charlie 01-24-2008, 08:39 AM Well, depending on the range or the amount of ciphers of the numbers you can store them into one numeric variable and divide it to extract a specific number. I mean: Numbers generated: 3, 7, 4, 2 X = (3*1000) + (7*100) + (4*10) + (2*1) Do yo see the way? To extract them: MsgBox X/1000 & X/100 & X/10 & X/1 You can avoid the '*1' and '/1'. I put it only for you to see the way I'm constructing the variable... lebb 01-24-2008, 09:03 AM Random number tutorial (http://www.xtremevbtalk.com/showthread.php?t=76270) ramil 01-24-2008, 06:26 PM I am randoming 4 numbers (0 to 3) and storing it in an array. How could i store them in the array with the condition that each array has a different value. JPB 01-24-2008, 09:10 PM There might be a better way, but I came up with this: Private Function RandomizeUnique(pLowerBound As Long, pUpperBound As Long) As String() ' Purpose : To return an array of random unique numbers from pLowerBound to pUpperBound ' Parameters : pLowerBound - The lowest number in the list of random numbers ' pUpperBound - The highest number in the list of random numbers ' Returns : A string array of unique random numbers from pLowerBound to pUpperBound Const cstrDelimiter As String = vbTab ' To separate randomly generated numbers in our ' string buffer Dim r As Long ' A random number for pLowerBound to pUpperBound Dim i As Long ' Just a counter Dim strRnd As String ' Holds the string of unique random numbers Dim strTmp As String ' Temporary string of padded random number Dim astrRnd() As String ' Array of unique random numbers If pLowerBound > pUpperBound Then ' The LowerBound cannot be greater than the UpperBound Err.Raise 5 End If For i = pLowerBound To pUpperBound ' Loop through all the numbers Do r = Int((pUpperBound - pLowerBound + 1) * Rnd + pLowerBound) ' Get a random number from pLowerBound to pUpperBound strTmp = r & cstrDelimiter ' Pad it Loop While InStr(1, strRnd, strTmp) > 0 ' Get another random number if the last generated number was already added to the string strRnd = strRnd & strTmp ' We found a free random number, so append it to our list Next i astrRnd = Split(RTrim$(strRnd), cstrDelimiter) ' Split the random numbers into an array. ' We use RTrim$ to remove the trailing space ' and avoid a blank element at the end of the array RandomizeUnique = astrRnd ' Return the array of unique random numbers End Function Private Sub Command1_Click() ' Let's test our RandomizeUnique function Dim astrRnd() As String Dim i As Long astrRnd = RandomizeUnique(0, 3) For i = LBound(astrRnd) To UBound(astrRnd) Debug.Print astrRnd(i), ; Next i Debug.Print End Sub The good: It is generic - you can generate a unique list of random numbers for any sequential range of long integers The bad: It uses strings (and unfortunately returns a string array instead of a long array, so you may have to do conversion depending on your needs). The string concatenation would get slow for really long lists, but should be ok for short ones (e.g. 0 to 3). lebb 01-25-2008, 06:35 AM If you will read the 2nd post in the thread I linked to before, you'll find a very clear and efficient example. :-\ |
EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum