Random numbers

Hi,

I have a peace of code i want to submit, but i think it's too small to rlly put it in the source code/articles section. I've made a really simple random number generator, and it goes a little something like this:

int main()
{
//clock time
clock_t cs=clock();
//loop some time away
for (int a=0;a<1200;a++) {
printf("Number: %i\t",log(a));
}
//clear screen
system("cls");
//save time difference
float tn=(float)(clock()- cs);
//print it out with integer-like
printf("\nRandom timenum: %.0f\n",tn);
//pause so you can see it
system("PAUSE");
return EXIT_SUCCESS;
}

It uses the time of printing "Number: " + the calculation 1000 times, witch is different allmost every time, depending on how busy your cpu is. The problem is that you can't really set a maximum of minnimum, but that doesn't really matter. I just think it is fun, i haven't seen the clock() function been used like this before.
Last edited on
(almost) All pseudo-random number generators use the system clock as one of their inputs.
Yes but this one doesn't really calculate the number it just checks the time it took. It's a lot less complicated.
So, surely it won't be all that random? Slower computers will generate bigger numbers.
this one may be a better solution i tested it in many ways

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
#include <iostream.h>
#include <conio.h>


unsigned last_rand;


unsigned short random() {
	unsigned long res, aux, count, beg, end;
	time_t t;
	beg = rand() % 3;
	end = rand() * 7;
	for(count = beg; count < end; count += clock() % 19) {
		gmtime(&t);
		aux += clock() + t;
	}
	res = (aux + last_rand) % ((unsigned long) USHRT_MAX);
	last_rand = res;
	return res;
}


unsigned short random(unsigned short min, unsigned short max) {
	return (random() % (max - min + 1)) + min;
}


void main() {
	while(1) {
		printf("%u\n", random(1, 6));
		getch();
	}
}


this example is a sort of dice game but the random functons are good for general purpose.
Hope this can helps you :)
Last edited on
Azrael spoke too quickly since he couldn't have meant that rand() (for example) uses the system clock in any way. It certainly does not.

However, in order to use rand() properly, you must "seed" it with srand(), often with this idiom, srand(time(0)), which passes it the current "seconds since the epoch".

As for the other posts, no practical RNG would have a loop in it. People do not want slower RNGs!

A better way than the clock to get a seed (or so I've heard) is to read a piece of a rapidly changing system file and hash it. Of course, this is not done every time to "get a random number" but just once (or occasionally) to seed the RNG, which then produces a pseudo-random sequence.
Last edited on
You have to manually seed rand()? Bleh.

However, almost all pseudo-random number generators use the system clock as one of their data sources. (The Linux random device, /dev/rand, certainly does.)
i'm a beginner i c++ too and i want to ask somethin' about time. if we want to make a script about countdown timer, for example, what the code would become? i want it to works like this...: if the time is 10.00 am, then i want to it countdown in the next 1 minute. that's is 10.01 am. and by that time, there's a "finish!" greeting displayin'...
Chipp: Here's a nice piece of code. It makes a count-down by implementing the wait() function.

http://www.cplusplus.com/reference/clibrary/ctime/clock.html
oh yeah, anyway, i think that there's a lot of library that i haven't learn yet. can you guys show me which page of this site that contain that. some kinda <fstream> <math.h> <conio.h> etc.
Topic archived. No new replies allowed.