Solved

Random Function

Posted on 1998-12-21
12
258 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

726 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