Solved

Random Function

Posted on 1998-12-21
12
251 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:spenner
Comment Utility
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
Comment Utility
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
Comment Utility
myqIG
If you don't have a definative answer, could you please post your comment as a "comment" not as an answer.

Thanks

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Expert Comment

by:jeffcameron
Comment Utility
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
Comment Utility
can you reject his answer so someone else can answer
0
 
LVL 13

Expert Comment

by:Mirkwood
Comment Utility
- 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now