|
-
Oct 20th, 2003, 02:36 AM
#1
Thread Starter
Fanatic Member
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.
-
Oct 20th, 2003, 02:55 AM
#2
Wel I don't think that those passwords are easy 2 remember.
-
Oct 20th, 2003, 03:22 AM
#3
Thread Starter
Fanatic Member
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
-
Oct 20th, 2003, 04:10 AM
#4
So Unbanned
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:
Dim mStr As String, RndStr As String
Dim x As Integer, y As Integer
mStr = "Hello World!"
For x = 2 To Len(mStr) - 1
y = Int(Len(RndStr) * Rnd) ' random position for character insertion
RndStr = Left$(RndStr, y) & Mid$(mStr, x, 1) & Right$(RndStr, Len(RndStr) - y)
Next
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.
-
Oct 20th, 2003, 04:58 AM
#5
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:
Function Rnd_In_Range(ByVal Upper_Limit As Integer, _
ByVal Lower_Limit As Integer, _
ByVal Exclude_List As Variant) _
As Integer
'Return a random number between "Lower_Limit" and "Upper_Limit", _
that does not appear in the array "Exclude_List"
'* Step 1 - find the numbers we can pick from
Dim available_numbers() As Integer '(the numbers we can use)
Dim num_available As Integer '(count of these)
Dim x As Integer, y As Integer '(for loops)
Dim is_excluded As Boolean '(is the number we are checking excluded?)
ReDim available_numbers(0 To Upper_Limit - Lower_Limit)
num_available = 0
'For each number in the range:
For x = Lower_Limit To Upper_Limit
is_excluded = False 'See if it is excluded..
For y = LBound(Exclude_List) To UBound(Exclude_List)
If Exclude_List(y) = x Then
is_excluded = True
Exit For
End If
Next y
If Not (is_excluded) Then '..if not, add to our list
available_numbers(num_available) = x
num_available = num_available + 1
End If
Next x
If num_available = 0 Then 'oops - no numbers available to pick from!
MsgBox "No numbers available!!"
'(you could handle this differently!)
Else
'* Step 2 - pick a number! (from 0 to num_available-1)
'NB: num_available is one higher than the last position in the array that _
we have used, but to get the random number to work properly we need _
to do this: Int((rnd(1) * (Upper-Lower + 1)) + Lower) _
which is the same as: Int((rnd(1) * (Upper-0 + 1)) + 0), _
or: Int(rnd(1) * (Upper + 1))
x = Int(rnd(1) * num_available)
'Return the selected number
Rnd_In_Range = available_numbers(x)
End If
'Get rid of the array
Erase available_numbers
End Function
Usage:
VB Code:
MsgBox Rnd_In_Range(10, 1, Array(1, 6, 2, 9, 4, 3))
-
Oct 28th, 2003, 06:33 AM
#6
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|