aboutsummaryrefslogtreecommitdiff
path: root/trand.c
blob: 54cdbb717edfdb61bb46d1462efc5f782601b2a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "trand.h"

int g_rfound;

void *thread(void *ma)
{
	static pthread_mutex_t mutex;
	pthread_mutex_t *m = &mutex;
	int r;
	for (unsigned long long i = 0; i < *(unsigned long long*)ma; i++)
		r  = rand();
	pthread_mutex_lock((pthread_mutex_t *)m);
	if (g_rfound == -1)
		g_rfound = r;
	pthread_mutex_unlock((pthread_mutex_t *)m);
	return (NULL);
}

/*
** Control how many threads to create,
** the more threads the more secure ntrand wil be
*/

int ntrand(unsigned long long n)
{
	g_rfound = -1;
	pthread_t threads[n];
	for (unsigned long long i = 0; i < n; i++)
		pthread_create(&(threads[i]), NULL, thread, (void *)(&i));
	for (unsigned long long i = 0; i < n; i++)
		pthread_join(threads[i], NULL);
	return (g_rfound);
}

/*
** Basically rand but we seed with time for you, user-friendly
*/

int strand()
{
	g_rfound = -1;
	srand(time(NULL));
	pthread_mutex_t mutex;
	pthread_t threads[DEF_NTHREAD];
	for (unsigned long long i = 0; i < DEF_NTHREAD; i++)
		pthread_create(&(threads[i]), NULL, thread, (void *)(&i));
	for (unsigned long long i = 0; i < DEF_NTHREAD; i++)
		pthread_join(threads[i], NULL);
	return (g_rfound);
}


/*
* In this version you are responsable for seeding rand()
*/

int trand()
{
	g_rfound = -1;
	pthread_mutex_t mutex;
	pthread_t threads[DEF_NTHREAD];
	for (unsigned long long i = 0; i < DEF_NTHREAD; i++)
		pthread_create(&(threads[i]), NULL, thread, (void *)(&i));
	for (unsigned long long i = 0; i < DEF_NTHREAD; i++)
		pthread_join(threads[i], NULL);
	return (g_rfound);
}