Generate & Write Random Values to Database
【Question】
I want to make a series of tables where each contains 25 values coming from a set of 30 values. How can I, quickly and randomly, produce these tables? I'm wondering if there is a way in Excel, or will I need to program something by myself? If there is, which language would be the easiest (Python, C or Java)?
Note: The 25 values would include no repeats. In other words, I'm looking for random combinations (30C25) of the values.
【Answer】
To generate random value combinations to update to the database, both Python and Perl take a roundabout route to do this. It’s much easier to get it done with esProc. Suppose table1 is the target database table, which consists of one field – field1, then the script is as follows:
A |
|
1 |
=["value1","value2"…"value30"] |
2 |
=A1.sort(rand()).to(1,25) |
3 |
=mySQLdb.update@a(A2,table1,filed1:~) |
A1: Define a sequence of 30 non-repeat values;
A2: Get 25 values from the sequence randomly;
A3: Write them to the database table.
If the values are 30 random records coming from a table, then the scrip is this:
A |
|
1 |
$select field1 from table1 |
2 |
=A1.sort(rand()).to(1,25) |
3 |
=mySQLdb.update@a(A2,table1) |
A1: Get random records from the table with a SQL statement and return them as a result set.
You can run a loop to write random values to multiple database tables:
A |
B |
|
1 |
=["value1","value2"…"value30"] |
=["tableA","tableB","tableC"] |
2 |
for B1 |
=A1.sort(rand()).to(1,25) |
3 |
=eval("myDB1.update@a(B2,"+A2+",c1:~)") |
More illustration can be found in Offline Dynamic Computation.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_Desktop/
SPL Learning Material 👉 https://c.scudata.com
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProcDesktop
Linkedin Group 👉 https://www.linkedin.com/groups/14419406/