?
Solved

Random Function

Posted on 1998-12-21
12
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 100 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses
Course of the Month8 days, 17 hours left to enroll

764 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