How do you do random numbers?

Status
Not open for further replies.

Princess Plushima

Princess of Dreams
Judge
I'm talking about C programming here by the way.

I'm just curious to know how you all generate random numbers, since there are alot of ways to do it, just no direct ways. Personally, I call the srand() function and pass it a value returned from time() in the time.h library.
Masses: Huh?
I use time() to get a different number for every second, then give that number to srand() which turns it into a random number.
Masses: Oh... Huh?
 
I don't program in C, but I know how SRB2 does it.

Getting random numbers from the clock takes time, so it creates a table of random numbers at startup. Then, it just sequentially loops through that table. Pseudo-random, true, but hey, it works!
 
brianv said:
I know how to do it in TGF
TGF?
hotdog003 said:
I don't program in C, but I know how SRB2 does it.

Getting random numbers from the clock takes time, so it creates a table of random numbers at startup. Then, it just sequentially loops through that table. Pseudo-random, true, but hey, it works!
Sounds a bit like a fake srand(), but whatever works. Though when I tried srand(time()) It didn't take any time at all.
 
Entirely redundant subject line

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int RandomInt(int max);

int main(int argc, char *argv[])
{
    int i;

    srand(time(NULL));

    /* Prints 100 random numbers in [1,1000]. */
    for(i=0; i<100; i++) printf("%d\n", RandomInt(1000));

    return 0;
}

/* Returns a random integer in [1,max]. */
int RandomInt(int max)
{
    return rand() % max + 1;
}

Normally I don't bother with a utility function like that, since at least half the time I want zero as my lower bound.

I imagine SRB2 uses a table for consistency purposes.
 
#include <math.h>

//code
int x = 23; //x = the highest number you want it to pick, including zero
int yourmom = rand(x);//including zero

OR

int yourmom = rand(x)+1; //not including zero, but obviously goes up to 24 at that point.
 
Lostgame said:
#include <math.h>

//code
int x = 23; //x = the highest number you want it to pick, including zero
int yourmom = rand(x);//including zero

OR

int yourmom = rand(x)+1; //not including zero, but obviously goes up to 24 at that point.
You do realise that that just generates the same random number for every time the program is run.
 
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top