Solved

Random Function

Posted on 1998-12-21
12
254 Views
Last Modified: 2010-05-03
i have to design a utility in VB that prompts for a name and then generates 5 other names including the entered name beginning with that letter. e.g user types Sarah - the program should provide 5 other names at random with letter s.

i know the rnd function is used for numbers but how would i go ablout doing the above.

thanks
0
Comment
Question by:spatel121798
  • 4
  • 2
  • 2
  • +4
12 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 1451735
I assume you've got a database or array of names available.

If you've got an array of names then you need to generate five different indexes at random.

I could write a function that does this


0
 

Author Comment

by:spatel121798
ID: 1451736
well i havent done anything yet but i could create a table in a database to hold the names.

if you could help with code that would be great.
0
 

Author Comment

by:spatel121798
ID: 1451737
well i havent done anything yet but i could create a table in a database to hold the names - or an array but im not very familiar with arrays.

if you could help with code that would be great.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 2

Expert Comment

by:spenner
ID: 1451738
Do you want actual names, or just random characters.  Because if you just want random characters, you can generate a number between 97 and 122 and then assign the character that corresponds to that ascii value.  ie.  97 = a, 98 = b, etc.  And you can get the text character by using the chr function.  ie chr(97)

However,if you want random names, your best bet is probably to create a database of them, and then pick one at random. (Select all the names, pick a random number between 0 and recordset size, do that many .movenext  and then use the selected name)
0
 
LVL 3

Accepted Solution

by:
myqlG earned 50 total points
ID: 1451739
You HAVE to have a .mdb full of names.. I would prolly have tables
a -z  and put the names in those tables depending on their
first name.. the random part is dependant on the number of
names in the .mdb...AND what you mean by random.
for example.. if this where in A:
abe
adam
Arthur
etc.
a TRULY random function could return adam.. all 4 times.
etc. etc.
so fill us in a little more please
0
 
LVL 2

Expert Comment

by:spenner
ID: 1451740
myqIG
If you don't have a definative answer, could you please post your comment as a "comment" not as an answer.

Thanks

0
 
LVL 1

Expert Comment

by:jeffcameron
ID: 1451741
ltrim the first letter off the enter name SimName = ltrim(Name,1)

use the leter in a select sql statement to get all the names that start with that letter
Select * From [Names] Where [Name] Like '" + Cstr(SimName) + "*" + "'"

then use your random number to multiply the count of the records returned.  rnd funciton returns a number from 0 to 1

round this number and that will be the index of the name in the returned recordset.  

i.e. say 5 names are returned - rnd = .2  - .2 * 5 = 1 get the first name.  

you may want to add the recordset to a collection so you can remove the name so the next name will not be the same.  you will then need to start with 4 * rnd.

you will have to take this approach because it is mathmatically impossible to randomly generate real names from letters.  however if you don't care if they are real names then it will be easier.  for instance you don't mind "fsdrgs" as a name.  I hope this is enough to get you started.
0
 
LVL 1

Expert Comment

by:jeffcameron
ID: 1451742
can you reject his answer so someone else can answer
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1451743
- Create ranges for A, B, C ,D ...
   so A = 0..10, B = 11..16, C=17..25
- Now pick 5 random numbers in this range using random function


   
 
0
 

Author Comment

by:spatel121798
ID: 1451744
Well i will start by setting up my .mdb of names. I will use jeffcamerons comment and then go from there. I will keep you posted if there are any problems.

thanks to all
0
 

Expert Comment

by:AndyJim
ID: 1451745
Heres some code


'Example CALL to my function

Private Sub Random5(ByRef a() As Integer)

    Dim c As Integer
    Dim d As Integer
    Dim iRandom As Integer
    Dim bFlag As Boolean

    For c = 1 To 5
        a(c) = -1
    Next
   
    For c = 1 To 5
   
        Do
       
            iRandom = Int(Rnd * 10 + 1)
           
            bFlag = True
       
            For d = 1 To c - 1
           
                If a(d) = iRandom Then bFlag = False
           
            Next
           
            a(c) = iRandom
           
        Loop Until bFlag
               
    Next

End Sub

Private Sub Command1_Click()

    Dim l(5) As Integer, c As Integer

    Call Random5(l)
   
    For c = 1 To 5
   
        MsgBox "#" & CStr(c) & "=" & CStr(l(c))
       
    Next

End Sub

0
 

Author Comment

by:spatel121798
ID: 1451746
I have connected my VB utility to my access database.I am generating a random number using the the random function. for example if the random nuber is 5 i want the form to bring up the record where nameID = 5.

Please can some tell me how to do this?

Thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question