Results 1 to 6 of 6

Thread: Generate Unique Random Integer in given range

  1. #1

    Thread Starter
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648

    Generate Unique Random Integer in given range

    I am sure there must be a function available that can generate a unique random integer in a given range.

    Inputs:

    Upper limit
    Lower Limit
    Array of integers between upper and lower limit.

    Output:

    A random integer that does not exist in the [array of integers between upper and lower limit], but is not greater than the upper limit or not less than the lower limit.

    Example:

    Upper limit = 10
    Lower limit = 1
    Input Array = {1,6,2,9,4,3}

    An allowed output is an element of the set {5,7,8,10)

    If you have the solution I would be most grateful.

    I received an email that shows that it does not matter in what sequence letters occur in a word, but providing the first and last letters of the word are the same, then you will be able to read the word correctly without even having to think about the order of the letters. So I am going to use this principle as part of a password generator to generate passwords that are easy for users to remember. An example:

    My nmae is Mike and I am a cmptuoer pmeoramrgr.

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    Wel I don't think that those passwords are easy 2 remember.
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  3. #3

    Thread Starter
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648
    Aoccdrnig to a rscheearch at an Elingsh uinervtisy, it deosn't mttaer
    in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht frist and lsat ltteer is at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit a porbelm. Tihs is bcuseae we do not raed ervey lteter by it slef but the wrod as a wlohe. So trehe is hpoe for all of us

    Perhaps mine was a bad example. And anyway, I can still use the concept for other uses

  4. #4
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by MikkyThomeon
    Aoccdrnig to a rscheearch at an Elingsh uinervtisy, it deosn't mttaer
    in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht frist and lsat ltteer is at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit a porbelm. Tihs is bcuseae we do not raed ervey lteter by it slef but the wrod as a wlohe. So trehe is hpoe for all of us

    Perhaps mine was a bad example. And anyway, I can still use the concept for other uses
    Uitnl the wdros get ralley msesd up.

    Tehn waht?

    How auobt opntnmioet?

    Too bad VB doesn't know:

    On Erorr Rsemue Nxet

    However!

    VB Code:
    1. Dim mStr As String, RndStr As String
    2. Dim x As Integer, y As Integer
    3. mStr = "Hello World!"
    4. For x = 2 To Len(mStr) - 1
    5. y = Int(Len(RndStr) * Rnd)  ' random position for character insertion
    6. RndStr = Left$(RndStr, y) & Mid$(mStr, x, 1) & Right$(RndStr, Len(RndStr) - y)
    7. Next
    8. RndStr = Left$(mStr, 1) & RndStr & Right$(mStr, 1)

    You'd have to pass individual words to this to process sentences.

    For anything more, you'll need to use a byte array.
    Last edited by DiGiTaIErRoR; Oct 20th, 2003 at 04:21 AM.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    it would help if you used proper grammar!

    it should be "first and last letters are in the right place", and there should be a few more commas throughout

    Anyway, that's enough pedanticism!


    your own method for solving it can be done in 2 steps, first of all build a list of numbers that can be used, then pick one of them.
    VB Code:
    1. Function Rnd_In_Range(ByVal Upper_Limit As Integer, _
    2.                       ByVal Lower_Limit As Integer, _
    3.                       ByVal Exclude_List As Variant) _
    4.                       As Integer
    5. 'Return a random number between "Lower_Limit" and "Upper_Limit", _
    6.  that does not appear in the array "Exclude_List"
    7.  
    8.  
    9. '* Step 1 - find the numbers we can pick from
    10. Dim available_numbers() As Integer  '(the numbers we can use)
    11. Dim num_available As Integer        '(count of these)
    12. Dim x As Integer, y As Integer      '(for loops)
    13. Dim is_excluded As Boolean          '(is the number we are checking excluded?)
    14.    
    15.     ReDim available_numbers(0 To Upper_Limit - Lower_Limit)
    16.     num_available = 0
    17.                             'For each number in the range:
    18.     For x = Lower_Limit To Upper_Limit
    19.         is_excluded = False    'See if it is excluded..
    20.         For y = LBound(Exclude_List) To UBound(Exclude_List)
    21.             If Exclude_List(y) = x Then
    22.                 is_excluded = True
    23.                 Exit For
    24.             End If
    25.         Next y
    26.    
    27.         If Not (is_excluded) Then   '..if not, add to our list
    28.             available_numbers(num_available) = x
    29.             num_available = num_available + 1
    30.         End If
    31.     Next x
    32.  
    33.  
    34.     If num_available = 0 Then   'oops - no numbers available to pick from!
    35.         MsgBox "No numbers available!!"
    36.         '(you could handle this differently!)
    37.        
    38.     Else
    39.      
    40. '* Step 2 - pick a number!  (from 0 to num_available-1)
    41.  
    42. 'NB: num_available is one higher than the last position in the array that _
    43.      we have used, but to get the random number to work properly we need _
    44.      to do this:            Int((rnd(1) * (Upper-Lower + 1)) + Lower) _
    45.      which is the same as:  Int((rnd(1) * (Upper-0 + 1)) + 0), _
    46.                        or:  Int(rnd(1) * (Upper + 1))
    47.         x = Int(rnd(1) * num_available)
    48.  
    49.         'Return the selected number
    50.         Rnd_In_Range = available_numbers(x)
    51.  
    52.     End If
    53.  
    54.                             'Get rid of the array
    55.     Erase available_numbers
    56.  
    57. End Function

    Usage:
    VB Code:
    1. MsgBox Rnd_In_Range(10, 1, Array(1, 6, 2, 9, 4, 3))

  6. #6

    Thread Starter
    Fanatic Member MikkyThomeon's Avatar
    Join Date
    Oct 2002
    Location
    At work...
    Posts
    648
    THanks for responding all - The last post answered me 150%

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width