Skip to content

Commit

Permalink
add getpid to more random number for seed calc
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAlabar committed Feb 7, 2025
1 parent 9465c89 commit fa802f8
Showing 1 changed file with 20 additions and 45 deletions.
65 changes: 20 additions & 45 deletions srcs/engine/bsp/bsp_seed.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,21 @@
#include <bsp.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>

/*
** Generates random seed value using rand()
** Seeds with time if not initialized
*/
unsigned int generate_random_seed(void)
{
static int initialized = 0;

if (!initialized)
{
srand(time(NULL));
srand(time(NULL) * getpid());
initialized = 1;
}
return ((unsigned int)rand() % (BSP_MAX_SEED - BSP_MIN_SEED + 1)
+ BSP_MIN_SEED);
}

/*
** Swaps two BSP lines positions in array
*/
static void swap_lines(t_bsp_line **lines, int i, int j)
{
t_bsp_line *temp;
Expand All @@ -43,17 +37,12 @@ static void swap_lines(t_bsp_line **lines, int i, int j)
lines[j] = temp;
}

/*
** Shuffles BSP lines array using Fisher-Yates algorithm
*/
void shuffle_lines(t_bsp_line **lines, int count, unsigned int seed)
{
int i;
unsigned int j;

if (!lines)
return ;
if (count <= 1)
if (!lines || count <= 1)
return ;
srand(seed);
i = count - 1;
Expand All @@ -65,67 +54,53 @@ void shuffle_lines(t_bsp_line **lines, int count, unsigned int seed)
}
}

/*
** Evaluates quality of a specific seed for partitioning
** Tests multiple partitions and calculates average score
*/
t_fixed32 evaluate_seed_quality(t_bsp_line **lines, int count,
unsigned int seed, int depth)

t_fixed32 evaluate_seed(t_bsp_line **lines, int count,
unsigned int seed, int depth)
{
t_bsp_line **test_lines;
t_fixed32 total_score;
int i;
int test_count;
t_bsp_line **test_lines;
t_fixed32 total_score;
int i;
int test_count;

test_lines = ft_calloc(count, sizeof(t_bsp_line *));
if (!test_lines)
return (INT32_MAX);
ft_memcpy(test_lines, lines, sizeof(t_bsp_line *) * count);
shuffle_lines(test_lines, count, seed);
total_score = 0;
if (count < 5)
test_count = count;
else
test_count = 5;
test_count = (count < 5) ? count : 5;
i = 0;
while (i < test_count)
{
total_score = fixed32_add(total_score,
eval_partition(test_lines[i], test_lines, count, depth));
eval_partition(test_lines[i], lines, count, depth));
i++;
}
free(test_lines);
return (fixed32_div(total_score, int_to_fixed32(test_count)));
}

/*
** Finds best seed by testing multiple options
*/
unsigned int find_best_seed(t_bsp_line **lines, int count, int depth)
{
unsigned int current_seed;
unsigned int best_seed;
t_fixed32 current_score;
t_fixed32 best_score;
int test_seeds;
int attempts;
unsigned int current_seed;
t_fixed32 current_score;

if (!lines)
return (BSP_MIN_SEED);
if (count <= 1)
return (BSP_MIN_SEED);
best_seed = generate_random_seed();
best_score = evaluate_seed_quality(lines, count, best_seed, depth);
test_seeds = 10;
while (test_seeds > 0)
best_score = evaluate_seed(lines, count, best_seed, depth);
attempts = 10;
while (attempts-- > 0)
{
current_seed = generate_random_seed();
current_score = evaluate_seed_quality(lines, count, current_seed, depth);
current_score = evaluate_seed(lines, count, current_seed, depth);
if (current_score < best_score)
{
best_score = current_score;
best_seed = current_seed;
}
test_seeds--;
}
return (best_seed);
}
}

0 comments on commit fa802f8

Please # to comment.