Solved

Random Function

Posted on 1998-12-21
12
255 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

790 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