C - Random Number Generator!

Status
Not open for further replies.

george_1988

Solid State Member
Messages
16
Hello,

I'm somewhere between beginner and intermediate when it comes to writing C code and really need a random number generator for a small project I have been working on.

I've been searching for ages and can't find any WORKING code to get a random number generator!

If anybody could help me out with this I would appreciate it very much. I am looking for a generator that is seeded by the clock because I want a different number everytime the program runs. Note: I am looking for C, not C++.

Thank you!
 
Couldn't you just call srand(time(0)) before you call rand()

This would seed the rand function with the current system time instead of using the default value of 1 for the seed.
 
Technically speaking there is no true random number generator. We studied it for a month in my C++ Advanced Algorithms course in college :p. You could however use srand like bla said and use a null value for time.

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

int main()
{

srand(time(NULL));
printf("%d", rand() % 10000);

}

Something like that will generate a number out of 10000 each time you run it. Just put it in a for loop to run it X amount of times.
 
Just use a vbs heres a code that i use somethimes:
Randomize()
Number = Int(HIGHESTnumberYOUwant * rnd())
call msgbox("Random Number" & Number,16,"Random Num")
 
Status
Not open for further replies.
Back
Top Bottom