# Random Generator

Tip: This page explains in detail how to create a random generator and when such a generator can be used. You can then find concrete examples in the instructions for the individual applications (see below).

The random generator is a functional building block for the questionnaire. The element draws one from a given list of elements ( notes ) and stores its value into a variable. The drawn value can be read using `value()` and used in a filter to randomly vary content of the questionnaire. For example, participants can be randomly assigned to experimental groups (Randomization).

By default, the random number generator ensures that all slips are drawn the same number of times (Drawing Type of drawing). This is important especially if the experimental groups are to be of equal size. With unrestricted random selection, on the other hand, it can happen (especially with manageable samples) that the group sizes deviate massively.

Tip: If the random variation does not work as desired, the debug information in the questionnaire usually provides a quick answer. For more information see Problem Solution for Filters and Explanatory video Experimental Randomization at the end.

There are several ways how to use a random generator. This are some functions which the random generator can execute:

• Two values are available for the drawn note: First, the numeric code and second, the entered text for the slip – this can be, for example, the filename of an image (e.g. Single Factorial Design).
• The random generator can draw multiple slips within an interview (option Codes drawn per interview), ensuring that the same slip is not drawn multiple times.
• The random generator returns only one value (and its label) per slip. But combinations can also be implemented using randomization (see Randomization: within-subject (Measurement Repetition)).

## Create a Random Generator

Here is explained how to create a simple random generator in general. How this random generator must then be used in particular is then explained in more detail in the individual chapters (see links below).

### Step 1: Add a Random Generator

The random generator as a functional building block is created as a new question in a rubric (An Online Questionnaire in 5 Minutes). Select by adding the question the questiontype Random Generator. Therefore you can tipe the first letters so the questiontype will accure in the selection list. Give the question a description and a question ID and click save ().

You need the question ID in following PHP-codes.

In the next step you enter the Codes (Contents) from which you want to draw later.

For example, if you want to randomize the participants into different groups (e.g., in Randomization), just enter the group names.

```Example:
Group 1
Group 2
Control group ```

By saving the question, the system now automatically assigns values (starting with 1) to the individual labels:

```Example:
1= Group 1
2= Group 2
3= Control group```

Now, when the random number generator is placed in the questionnaire, it assigns a random value (slip of paper) to the participant. These values can be read out later and used in filters.

### Step 3: More Settings

In some cases it is necessary to adjust the settings in the random generator:

• Number of notes per interview
How many notes have to be drawn per interview? In some cases it is necessary to draw several slips, for example to present 20 images from a pool of 300 images. Enter the corresponding value under Number of slips per interview.
• Type of Drawing
Think in advance what draw you need. By default, equal distribution is provided. This makes sense for most cases.
• The uniformly distributed draw may result in unequal groups in the adjusted dataset if the dropout rate is high. It therefore makes sense to place the random number generator further back in the questionnaire, e.g. where the stimulus is presented. For correction see readjust drawings.
• Important: Select the option “Equal distribution in completed questionnaires” only if the survey extends over a longer survey period and the questionnaire can be completed in a short time (FINISHED=1). Otherwise, if many participants take part in a short period of time (measured by the time between random drawing and completion of an interview), this can lead to unequal distributions. In this case better chose the normal equally distributed draw.
• BIn the case of random draw with discarding, you must expect, especially in small samples (less than approx. 250 per study group), that the respondents will be distributed unevenly among the study groups as part of the statistical selection error.
• Show contents it is also possible to already specify file names as slips and use Show contents to include the associated images on the questionnaire page without any further PHP code. See more under Single Factorial Design.

### Step 4: Placing the Random Generator

Depending on the further use of the values of the random generator, it must be placed appropriately in the questionnaire. To do this, the question must be dragged to one side when Compose Questionnaire. In any case, the value must be placed before a PHP code where the value is to be read. Once the value of the random generator has been drawn, this value can be read out again in the questionnaire at any time.

## Using the Random Generator

The random generator is needed as a basis for some dynamic content in SoSci Survey. This includes:

If one works with many experimental groups, it can be that the actually successful equal distribution is lost after the adjustment. Especially if the number of cases per group is very small, the cases that have to be deleted during the cleanup (e.g. due to the manipulation check) are significant. Then it may be necessary to slightly adjust the random number generator during the survey.

Attention: Readjustment of random selection should only occur when participants are recruited continuously and from the same population. Otherwise there is a risk of confounding: Imagine an experiment with 4 groups, where the participants are all invited on a certain day. After 2/3 of the participants have completed the questionnaire, one notices that in group 1 an above-average number of cases need to be cleaned up. So from now on more participants will be assigned to group 1. Now there is confounding because participants who respond later to an invitation may be significantly different from participants who respond quickly. So participants in Group 1 would now differ a priori from participants in other groups – and thus differences in the dependent variables would no longer be due to the experimental stimulus alone.

Tip: In case you cannot adjust the random draw afterwards, you can use a urn and configure in `urnDraw()` a manual dropping of the codes. Manual filing using `urnPutAway()` is only done if a filter has been used to verify that the case is usable. Disadvantages: It is comparatively time-consuming and, especially at the beginning of the survey, there may be significantly unequal group sizes.

If the conditions for readjustment (see above) are met, the procedure is as follows:

1. The preliminary dataset is cleaned (dropouts, missing responses, manipulation check, no consent to use the information, …).
2. A frequency table is created over the variable of the random generator.
3. This frequency table is converted into a format `Code: Number` (see below).
4. This list is entered in the random generator into the field Previously drawn – the order of the values does not matter, it is only important that code and number are included in each case.
5. After saving, the list is automatically sorted and further random draws are made in such a way that previous inequalities are compensated.

For example, the list of codes and usable cases may look like the following:

```111111: 2
111211: 1
111311: 2
111411: 2
112111: 1
112211: 2
112311: 2
112411: 2
u.s.w.```

In Excel or OpenOffice Calc, you create such a list by merging the previously created frequency table (with codes, not labels) with the following command (when column A contains the codes and column B contains the frequencies):

`=CONCATENATE(A1; ": "; B1)`

In GNU R you can output frequency table and list as follows (if the variable of the random generator is called e.g. RG01):

```tmp = table(data\$RG01)
cat(paste(paste(names(tmp), tmp, sep=": "), collapse="\n"))```