如何从一个表中的不同组中选择随机主题? [英] How do I select random subjects from different groups in one table?

查看:78
本文介绍了如何从一个表中的不同组中选择随机主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,从20个组中随机选择20个科目。所有小组和科目都在一个表格中。组的变量是SecondaryID,主题的变量是SubjectID。我发现一些在线代码适用于随机选择,但无法修改代码来为每个SecondaryID执行此操作。任何建议都将不胜感激。


谢谢!

I am trying to write a program to randomly select 20 subjects from 20 groups. All groups and subjects are in one table. The variable for group is SecondaryID and the variable for subject is SubjectID. I found some code online that works for the random selection but have been unable to modify the code to do this for each SecondaryID. Any suggestions would be much appreciated.

Thanks!

展开 | 选择 | 换行 | 行号

推荐答案

代码适合我。我注意到部分Tablename的var SecondaryID为空(未初始化)。我会再检查一下。 (现在不能)。检查数据类型(我刚使用长整数)并检查拼写等。查找一个只有日期的表。
The code works for me. I noticed the var SecondaryID for part of the Tablename is empty (not initialized). I''ll check it somemore though. (can''t now). Check data types (I just used "long integer") and check spelling, etc. Look for a table with just the date.


你甚至不想要的是什么,但我猜你会有一个表格,其中有20个可能的分组记录,你希望从这些组中随机选择一个主题。如果那不能正确表达,那么你需要自己重试,因为没有一个有意义的问题,答案很难得到。


因为你也没有提供任何信息指出主题和组值的格式,我想我们应该假设它是文本的。因为使用 Rnd()函数产生随机数据,该函数返回一个值,使得 0< = X< 1 ,我们需要以某种方式将这样的值转换为为每个Group值选择n条记录之一的值,其中n是该组的记录数。


我建议Recordset处理可用于确定要选择的实际记录。确定每个组的记录数( DCount()可以用于此处理或通过记录集处理以计算匹配的记录)然后将其乘以生成的随机数并回滚到整数值。这将是从集团的第一个开始的记录数量。


您可以生成一个PK ID列表,包含在 In()在找到它们时列出或处理它们。
It''s not very clear what you even want, but I''ll guess you have a table with records split across 20 possible groups, and you''re looking to select one subject from each of these groups, randomly. If that doesn''t express it properly then you need to try again yourself, because without a question that makes sense, answers are hard to come by.

As you also supply no information to indicate what format Subject and Group values are in I suppose we should assume it''s textual. As random data is produced using the Rnd() function, which returns a value such that 0 <= X < 1, we need, somehow, to convert such a value into something that will select one of n records for each Group value, where n is the number of records for that Group.

I would suggest Recordset processing might be used to determine the actual record to select. Determine the number of records for each Group (DCount() could be used for this or processing through the recordset in order counting the matching records) then multiply this by the generated random number and round back to an integer value. This will be the number of records to move on to from the first of the Group.

You can produce a list of PK IDs to include within an In() list or process them as you find them.


您不需要创建临时表,创建字段,对临时表进行排序,然后删除临时表。以下代码基于NeoPa的Recordset处理理念,将根据该组的主题数量为每个唯一组选择一个随机主题。我已经发布了一些示例数据,代码逻辑以及3次试运行的结果:
You do not need to Create a Temporary Table, Create a Field, Sort the Temporary Table, then Delete the Temporary Table. The following Code, based on NeoPa''s idea of Recordset processing, will pick a Random Subject for each Unique Group, based on the Number of Subjects for that Group. I''ve posted some Sample Data, the Code Logic, as well as Results for 3 Trial Runs:
  1. tblData
  1. tblData
展开 | 选择 | Wrap | 行号


这篇关于如何从一个表中的不同组中选择随机主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆