Posted: . At: 10:42 AM. This was 3 years ago. Post ID: 15042
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


A nice example of C programming. Getting a random number in milliseconds.


This is an interesting version of my random monster program. I added a simple function to get a random number in milliseconds.

randommonster.c
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
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
 
char *cuserid(char *s);
 
const char* x[] = {
    "Baron of Hell", "Demon", "Hellknight",
    "Cyberdemon", "Mancubus", "Revenant",
    "Heretic Imp", "Zombieman", "Sergeant",
    "Beholder", "Moloch", "Satyr",
    "Afrit", "Ettin", "Maulator",
    "Hectebus","Cacodemon","SabreClaw"
};
 
int64_t milliseconds() /* Getting the time in milliseconds. */
{
    struct timespec now;
    timespec_get(&now, TIME_UTC);
    return ((int64_t) now.tv_sec) * 1000 + ((int64_t) now.tv_nsec) / 1000000;
}
 
int cool(void) {
    int k;
 
    size_t i = sizeof(x) / sizeof(x[0]); /* Getting the size of the array as an integer. */
    k = 0;
    srand((unsigned)milliseconds());
    k = rand() % i;
    return k;
}
 
int main() {
 
  char myname[128];
  char *player;
  cuserid(myname);
 
  printf("%s was slaughtered by a %s.\n",myname , x[cool()]);
 
  return 0;
}

This is better than just seconds if a program is executed more often, this can get closer to true randomness. Even though computers are not the best at generating random numbers, you can try your best and get as close as possible.

Print the millisecond’s value this way.

printf("%u\n\n", milliseconds());

1 thought on “A nice example of C programming. Getting a random number in milliseconds.”

  1. Rid yourselves, then, of all spite, deceit, hypocrisy, envy and carping criticism

    He is the living stone, rejected by human beings but chosen by God and precious to him; set yourselves close to him so that you, too, may be living stones making a spiritual house as a holy priesthood to offer the spiritual sacrifices made acceptable to God through Jesus Christ.

    To you believers it brings honour. But for unbelievers, it is rather a stone which the builders rejected that became a cornerstone,a stumbling stone, a rock to trip people up. They stumble over it because they do not believe in the Word; it was the fate in store for them.

    But you are a chosen race, a kingdom of priests, a holy nation, a people to be a personal possession to sing the praises of God who called you out of the darkness into his wonderful light.

    Once you were a non-people and now you are the People of God; once you were outside his pity; now you have received pity. I urge you, my dear friends, as strangers and nomads, to keep yourselves free from the disordered natural inclinations that attack the soul.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.