Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 181 Vote(s) - 3.72 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to 'randomize()' random numbers in C(Linux)?

#1
Trying to generate random numbers in C, rand() doesn't generate different numbers each time i compile the code, can anyone tell me how to use srand() or tell any other method for generating.
Reply

#2
This is commonly used solution:

srand ( time(NULL) );

Execution of all C code is deterministic, so you have to bring in something that is different every time you call the `srand`. In this case it is time.

Or you can read data from [`/dev/random`][1] (open it just like any other file).


[1]:

[To see links please register here]

Reply

#3
Be careful; the `rand(3)` manpage on linux notes that `rand()` implementations on some platforms do not give good randomness on the lower-order bits. For this reason, you might want to use a library to acquire real random numbers. [Glib][1] provides useful functions like `g_random_int_range()` which may better suite your purpose.


[1]:

[To see links please register here]

Reply

#4
In order to generate a sequence of [pseudorandom][1] numbers, the generator needs to be [seeded][2]. The seed fully determines the sequence of numbers that will be produced. In C, you seed with `srand`, as you indicate. According to the `srand(3)` man page, no explicit seeding implies that the generator will use 1 as a seed. This expains why you always see the same numbers (but do remember that the sequence itself is pretty random, with quality depending on the generator used, even though the sequence is the same each time).

User mzabsky points out that one way to get a seed that feels random to a human user is to seed with time. Another common method (which I just saw that mzabsky also points out - sorry) is to seed the generator with the contents of the system's random number generator, which draws from an entropy pool fed by things such as mouse movement, disk timings etc. You can't draw a lot of randomness from the system generator, as it won't be able to gather enough entropy. But if you just draw a seed from it, you'll have chosen at random a sequence of random numbers in your program. Here's an example of how to do that in C on Linux:

unsigned int seed;
FILE* urandom = fopen("/dev/urandom", "r");
fread(&seed, sizeof(int), 1, urandom);
fclose(urandom);
srand(seed);

In light of Conrad Meyer's answer, I thought I'd elaborate a bit more. I'd divide the use of random numbers into three categories:

1. **Variation.** If you use random numbers to create seemingly random or varied behavior in for example a game, you don't need to think very hard about the topic, or about choosing a proper seed. Seed with time, and look at some other solution if this turns out not to be good enough. Even relatively bad RNGs will look random enough in this scenario.
2. **Scientific simulations.** If you use random numbers for scientific work, such as Monte Carlo calculations, you need to take care to choose a *good generator*. Your seed should be fixed (or user-changeable). You *don't* want variation (in the sense above); you want deterministic behavior but good randomness.
3. **Cryptography.** You'll want to be extremely careful. This is probably out of the scope of this thread.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#5
<!-- language-all: c -->

If you are using an OS that does not provide /dev/random then use something like what is shown below


timeval t1;
gettimeofday(&t1, NULL);
srand(t1.tv_usec * t1.tv_sec);


This piece of code can be ported easily to other OS.

To improve the seed- you can combine (maybe using MD5 or a checksum algorithm) time product shown above with a MAC address of the host machine.


timeval t1;
gettimeofday(&t1, NULL);
unsigned int seed = t1.tv_usec * t1.tv_sec;

unsigned char mac_addr[6];
getMAC(&mac_addr);
improveSeedWithMAC(&seed, mac_addr) ; // MD5 or checksum ...

srand(seed);
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through