-1

How to write a random generator function without using standard C functions like time functions, static variables or global variables?

/* #include <time.h> Don't use time functions. */
/* int seed = 1234; Don't use global variables. */
int generate_random(void)
{
    /* static int seed = 1234; Don't use static variables. */
    /* return (time() % 100); Don't use time functions. */
}

In other words, this random generator function should not remember the last state.

Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
  • You've to design one, then try writing... – rakib_ Aug 22 '13 at 06:23
  • 1
    Here: [Generate Random numbers without using any external functions](http://stackoverflow.com/questions/15038174/generate-random-numbers-without-using-any-external-functions/15040471#15040471) I have suggested some source to useful to write random function – Grijesh Chauhan Aug 22 '13 at 06:24
  • What is the objective? If it is to be truly random, it should be based on a hardware noise generator. On some operating systems, there is a device which collects random data which could be used.... – wallyk Aug 22 '13 at 06:24
  • You'll need to use /dev/random – Stolas Aug 22 '13 at 06:27
  • I don't understand what you mean by time function. But will you consider using something like this? `int random, iGuess; srand (time(NULL)); /* initialize seed: */ random = rand() % 10 + 1; /* Random number between 1 and 10. */` – Thanushan Aug 22 '13 at 06:30
  • 3
    If you are writing the function and it is not going to remember any state, then your function has to be able to get random data from a good random source. The standard location is `/dev/random` or `/dev/urandom` on systems that support these. Or you need a custom device that generates random numbers for you. If you're generating your own pseudo-random data, you will need global or static variables to store prior state. – Jonathan Leffler Aug 22 '13 at 06:38
  • @JonathanLeffler: Or passing previous state through a parameter, which the "template" the OP gives doesn't seem to allow. – Michael Foukarakis Aug 22 '13 at 07:32
  • Is it acceptable to rely on an external provider of randomness which will use its own static state which you can't see? In that case, use `/dev/random`, otherwise you must collect together entropy in real-time, and it will probably be very, very slow. – sh1 Aug 22 '13 at 08:32

5 Answers5

2

you can also use srand by

#include<stdlib.h>

srand(getpid());

int num = rand();
jinal shah
  • 122
  • 4
  • If the point of the exercise is to avoid global state, srand/rand won't work because they use global state internally. – user694733 Aug 22 '13 at 07:14
1

This solution meets all your criterion, but it does require the Internet.*

unsigned generate_random () {
    FILE *random;
    char cmd[512];
    unsigned number;

    snprintf(cmd, sizeof(cmd),
         "wget -qO - "
         "'http://www.random.org/integers/"
         "?num=1&min=0&max=%u&col=1&base=10&format=plain&rnd=new'",
         USHRT_MAX);

    random = popen(cmd, "r");
    fscanf(random, "%u", &number);
    pclose(random);

    return number;
}

*That's not really a mark against the algorithm, though, since the Internet is an excellent source of random data. ;-)

jxh
  • 69,070
  • 8
  • 110
  • 193
0

Assuming you need to do your own pseudo-random number generator, you need to remember the state.

If function shouldn't remember last state, you need to pass it as an argument:

int generate_random(int * lastState)
{
    // generate next random from lastState
    // store state to lastState
    // return result
}

int main(void)
{
    int lastState = 1234; // seed it
    int random = generate_random(&lastState);
}
user694733
  • 15,208
  • 2
  • 42
  • 68
-1

do

#include <time.h>
#include <stdlib.h>

srand(time(NULL));
int r = rand();

if you want to limit the number use

int r = minNum + rand() % maxNum;

do not try to create your own random function. it will not end well

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
-1

Guessing about your question...I have to ask what is the requirement for wanting a random seed?

Being able to set the seed value is a plus for code because you at least try to do things like regression testing.

If your question is about random numbers in general. There are two steps needed to generate a series of random numbers.

First, set a seed value, for example, srand(1234); where 1234 is the seed value. Your code only issues this instruction once. When you run the program a second time your code can re-use the same seed value, in which case the sequence of random numbers will be the same. If you set a different seed, then you will get a different sequence of random numbers.

The second step is to retrieve the random number values for use in your code.

One Possible Method

In theory, you could use a fixed seed value and then generate R random numbers, where R is some number taken from the system mod 100 (for example). Then use that R'th random number as the seed value for the actual sequence of random numbers for your application.

You say that you don't want to use Time(), but maybe in this double sequence of random numbers and the Time() mod n function will provide enough randomness.

Now, I say in theory, because random numbers from rand() are actually psuedo-random and for all intents and purposes they really are random (guys with math PhDs say this...), so I doubt if doing this "double selection" will make the numbers more random. But, this method or one like it might make a reviewer or boss feel more comfortable, but this would be a false sense of security.

JackCColeman
  • 3,777
  • 1
  • 15
  • 21