[DGD] File access question

Mikael Lind mikkelin at gmail.com
Sat Jul 14 00:23:10 CEST 2007


Hello Kurt,

The card game concept sounds interesting. Is this some sort of trading
card game? I have been thinking about using DGD to implement something
along those lines.

On 13/07/07, Kurt Nordstrom <kurt at blar.net> wrote:
> Is the loading of the entries something that I'd have to do with scheduled
> call outs to avoid a tick overflow?  And is there any clever way to pick
> 200 random lines out of a variable length file?

Here is one way, not sure if it fits your idea of clever:

1. Count the number of entries.
2. Generate an array with integers ranging from 0 to count - 1.
3. Shuffle the array.
4. Use the 200 first elements as line indices.

Here is how to shuffle an array:

void swap_elements(mixed *arr, int i, int j)
{
    mixed val;

    val = arr[i];
    arr[i] = arr[j];
    arr[j] = val;
}

mixed *shuffle_array(mixed *arr)
{
    int i, size;

    arr = arr[..];
    size = sizeof(arr);
    for (i = 0; i < size; ++i) {
        swap_elements(arr, i, i + random(size - i));
    }
    return arr;
}

Here is a unit test:

# define ASSERT(expr)  if (!(expr)) error("Assertion failed: " + #expr)

void test_shuffle_array()
{
    int *arr;

    arr = ({ 13, 14, 15, 16, 17, 18, 19, 20, 21 });
    ASSERT(sizeof(shuffle_array(arr) & arr) == sizeof(arr));
}

There are more clever ways of randomly selecting a fixed number of
entries from large data sets, but I am not sure on the details. The
above will probably solve your problem.

Regards,
Mikael



More information about the DGD mailing list