B4J Code Snippet Creating a Random Dummy Data Generator

Mashiane

Expert
Licensed User
Ola

So I am am working on my Mock app, after getting some of my list running as per discussion here, I managed to have some consensus of some doable things..

What happens is that a data structure is first defined, this will contain the field names and the dummy data types. Somewhere there should be an indication of how many data records to create. These can be converted to JSON or any other format that's needed.

Below is data generated in two samples done each time a refresh button is clicked. Well, due to the Birth Date being a date, we didn't set up a limit to the year thus years after 2019.

You will note that the code in this example is related to a BANano project but the added code module UOENowData applies to any package you can work with in terms of logic.

UOENowData.gif

My example loads the generated dummy records to a grid table. This is similar to the grid example I explained in [BANano] An intersting grid you might like].

In this case...

1. The dummy data structure is read from a saved table and then
2. Using the structure the dummy data is generated. For example, there is a birthdate field and its dummy data should be of format date. So the dummy data generator will create a random date
using UOENowData.Rand_Date method. You can check the code on that code module to see what that does. The generated lists for now just are enough to meet my needs, otherwise should one require more, they can then create additional lists etc.

B4X:
''set up the grid for tables
Sub ShowRecords(Result As List)
    'get the currently saved project
    Dim prj As Map = banano.GetSessionStorage(ActiveDB)
    'get the currently active table
    Dim tbl As Map = banano.GetSessionStorage(ActiveTable)
    'read the auto increment field
    Dim tableautoincrement As Boolean = tbl.Get("tableautoincrement")
    'read the primary key
    Dim tableprimarykey As String = tbl.Get("tableprimarykey")
    'how many records to generate as dummy data
    Dim sTableRecords As String = tbl.Get("tablerecords")
    'hold each dummy record
    Dim recs As List
    recs.Initialize
    'define the grid for records
    grdRecords.PrimaryKey = tableprimarykey
    'create the table columns
    For Each record As Map In Result
        Dim sfieldname As String = record.Get("fieldname")
        Dim sfieldshowontable As Boolean = record.Get("fieldshowontable")
        Dim sfieldsortby As Boolean = record.Get("fieldsortby")
        Dim sfieldtitle As String = record.Get("fieldtitle")
        Dim sfieldcolumntype As String = record.Get("fieldcolumntype")
        Dim sfieldcolumnalign As String = record.Get("fieldcolumnalign")
        'only generate dummy for everything to show on table
        If sfieldshowontable Then
            'add the column to the grid
            grdRecords.AddColumn(sfieldname,sfieldtitle,sfieldcolumntype,0,sfieldsortby,sfieldcolumnalign)
        End If
    Next
    'how many records do you want to create
    Dim tRecs As Int = Abs(sTableRecords)
    Dim cRecs As Int
    For cRecs = 1 To tRecs
        'each record will be a map
        Dim rec As Map
        rec.Initialize
        'put the primary key
        rec.Put(tableprimarykey, cRecs)
        For Each record As Map In Result
            Dim sfieldname As String = record.Get("fieldname")
            Dim sfieldshowontable As Boolean = record.Get("fieldshowontable")
            Dim sfielddummy As String = record.Get("fielddummy")
            Dim sfieldrange As String = record.Get("fieldrange")
            'only generate dummy for everything to show on table
            If sfieldshowontable Then
                'this is not the primary key
                If sfieldname <> tableprimarykey Then
                    Dim sValue As String = ""
                    'add the column to the grid
                    Select Case sfielddummy
                    Case "name"
                        sValue = UOENowData.Rand_Human_Name
                    Case "full name"
                        sValue = UOENowData.Rand_Full_Name
                    Case "country"
                        sValue = UOENowData.Rand_Country_Name
                    Case "capital city"
                        sValue = UOENowData.Rand_Capital_City
                    Case "email"
                        sValue = UOENowData.Rand_Email("icloud.com",True)
                    Case "gmail"
                        sValue = UOENowData.Rand_Gmail(True)
                    Case "yahoo"
                        sValue = UOENowData.Rand_Yahoo_Mail(True)
                    Case "gender"
                        sValue = UOENowData.Rand_Gender
                    Case "lorem ipsum"
                        sValue = UOENowData.LoremIpsum(2)
                    Case "phone"
                        sValue = UOENowData.Rand_Phone_Number(27,10)
                    Case "yes/no"
                        sValue = UOENowData.Rand_True_Or_False
                    Case "sport"
                        sValue = UOENowData.Rand_Sport_Name
                    Case "date"
                        sValue = UOENowData.Rand_Date
                    Case "time"
                        sValue = UOENowData.Rand_Time
                    Case "date time"
                        sValue = UOENowData.Rand_DateTime
                    Case "age"
                        sValue = UOENowData.Age
                    Case "pin code"
                        sValue = UOENowData.GeneratePinCode(4)
                    Case "company name"
                        sValue = UOENowData.Rand_Company_Name
                    Case "job title"
                        sValue = UOENowData.Rand_Occupation
                    Case "ip address"
                        sValue = UOENowData.Rand_Ip_Address
                    Case "money"
                        sValue = UOENowData.Rand_Money(4)
                    Case "street"
                        sValue = UOENowData.Rand_Home_Address
                    Case "number"
                    End Select
                    rec.Put(sfieldname,sValue)
                End If
            End If
        Next
        recs.Add(rec)
    Next
    'set the data source for the grid
    grdRecords.SetDataSource(recs)
    'refresh the table
    grdRecords.Refresh
    'convert the data to json
    Dim jsonData As String = App.List2Json(recs)
    'show the code on the page.
    records.AddPrismCode(4,1,"jsonRecs",jsonData,"json",False)
End Sub
The attached code module is the crux of the random dummy data generator.

Ok, let me rather do a simpler example. We want to create 10 records of random data based on some dats structure. To do this we will call the GenData subroutine. This will call RandomDataGen

B4X:
Sub GenData
'lets create the data structure we need
Dim lst As List
lst.Initialize
lst.add(createmap("fieldname":"id", "fielddummy":"number","fieldshowontable":true))
lst.add(createmap("fieldname":"names","fielddummy":"name","fieldshowontable":true))
....
....
'the primary key is id, generate 10 records with specified data structure)
Dim Records As List = RandomDataGen("id", 10, lst)
Log(Records)
End Sub

'generate the actual dummy records
Sub RandomDataGen(tableprimarykey as string, numRecs as Int, DataStructure As List) As List
dim recs As List
recs.initialize
Dim recCnt As Int
For recCnt = 1 to numRecs
    dim rec As Map = CreateMap(tableprimarykey:recCnt)
    For each record As Map in DataStructure
            Dim sfieldname As String = record.Get("fieldname")
            Dim sfieldshowontable As Boolean = record.Get("fieldshowontable")
            Dim sfielddummy As String = record.Get("fielddummy")
            'only generate dummy for everything to show on table
            If sfieldshowontable Then
                'this is not the primary key
                If sfieldname <> tableprimarykey Then
                    Dim sValue As String = ""
                    'add the column to the grid
                    Select Case sfielddummy
                    Case "name"
                        sValue = UOENowData.Rand_Human_Name
                    Case "full name"
                        sValue = UOENowData.Rand_Full_Name
                    Case "country"
                        sValue = UOENowData.Rand_Country_Name
                    Case "capital city"
                        sValue = UOENowData.Rand_Capital_City
                    Case "email"
                        sValue = UOENowData.Rand_Email("icloud.com",True)
                    Case "gmail"
                        sValue = UOENowData.Rand_Gmail(True)
                    Case "yahoo"
                        sValue = UOENowData.Rand_Yahoo_Mail(True)
                    Case "gender"
                        sValue = UOENowData.Rand_Gender
                    Case "lorem ipsum"
                        sValue = UOENowData.LoremIpsum(2)
                    Case "phone"
                        sValue = UOENowData.Rand_Phone_Number(27,10)
                    Case "yes/no"
                        sValue = UOENowData.Rand_True_Or_False
                    Case "sport"
                        sValue = UOENowData.Rand_Sport_Name
                    Case "date"
                        sValue = UOENowData.Rand_Date
                    Case "time"
                        sValue = UOENowData.Rand_Time
                    Case "date time"
                        sValue = UOENowData.Rand_DateTime
                    Case "age"
                        sValue = UOENowData.Age
                    Case "pin code"
                        sValue = UOENowData.GeneratePinCode(4)
                    Case "company name"
                        sValue = UOENowData.Rand_Company_Name
                    Case "job title"
                        sValue = UOENowData.Rand_Occupation
                    Case "ip address"
                        sValue = UOENowData.Rand_Ip_Address
                    Case "money"
                        sValue = UOENowData.Rand_Money(4)
                    Case "street"
                        sValue = UOENowData.Rand_Home_Address
                    Case "number"
                    End Select
                    rec.Put(sfieldname,sValue)
              Next
              recs.add(rec)
       Next
    Return recs
End Sub
Ta!

#HelpingOthers2Succeed

Credits: @FrostCodes
 

Attachments

Top