srand

Recommended way to initialize srand?

I need a 'good' way to initialize the pseudo-random number generator in C++. I've found an article that states: In order to generate random-like numbers, srand is usually initialized to some distinctive value, like those related with the execution time. For example, the value returned by the function time (declared in heade...

Will repeated calls to srand() in c++ use the same seed?

If I have srand(2) declared in my main of my driver file, do I need to declare srand(2) in my code file which is being linked with my driver? Thanks. edit (from user's comment below) If I do, srand(2); srand(2); will I get the seed as 2? or something else? ...

srand((unsigned)(time(NULL))); (rand())/(RAND_MAX/2) - 1 C# equivalent

What is the c# equivalent of the following c++: srand((unsigned)(time(NULL))); weight=(double)(rand())/(RAND_MAX/2) - 1; ...

Issues with seeding a pseudo-random number generator more than once?

I've seen quite a few recommendations for not seeding pseudo-random number generators more than once per execution, but never accompanied by a thorough explanation. Of course, it is easy to see why the following (C/C++) example is not a good idea: int get_rand() { srand(time(NULL)); return rand(); } since calling get_rand several ...

Can anyone see what is wrong with this (time related functions in C)

#include <stdio.h> #include <stdlib.h> #include <time.h> static struct tm createDate(unsigned day, unsigned mon, int year) { struct tm b = {0,0,0,day,mon-1,year-1900}; return b; } static int dateExceeded(unsigned day, unsigned mon, int year) { struct tm b = createDate(day,mon,year); time_t y = mktime(&b), now; tim...

Find out what a random number generator was seeded with in C++

I've got an unmanaged c++ console application in which I'm using srand() and rand(). I don't need this to solve a particular problem, but was curious: is the original seed passed to srand() stored somewhere in memory that I can query? Is there any way to figure out what the seed was? ...

Correct use of s/rand or Boost::random

I know this kind of question has been asked a few times, but alot of them answers boil down to RTFM, but I'm hoping if I can ask the right question... I can get a quasi-definitive answer for everyone else as well, regarding implementation. I'm trying to generate a sequence of random numbers in one of the two following ways: #include <c...

Is it possible to get an appriximtion to a seed based on a finite sequance of pseudo random numbers?

suppose I have some numbers that form a series for example : 652,328,1,254 and I want to get a seed that if I ,for example ,do srand(my_seed); I will get some kind of approximation with bounded error to my origonal sequance , when all numbers appearing in the same order. thanks, Alex ...

How can I store the state of the pseudo-random generator in Perl?

Is there a way to store the current state of the built in pseudo-random number generator in Perl so that when my program is run again, it can pick up the sequence from where it left off rather than starting with a new sequence? Right now, I am storing where I am as well as the initial seed and then throwing away the initial segment whic...

srand function in C

I am trying to code a random number generation function in embedded C where I can't include the math.h file. Thats why I'm not able to use the seed srand function. Is there any other way to seed it other than time? ...

C++ random number from a set

Is it possible to print a random number in C++ from a set of numbers with ONE SINGLE statement? let's say the set is 2, 5, 22, 55, 332 i looked up rand, but I double it's possible to do in a single statement ...

Random numbers in C

srand(time(NULL)); for(i = 0; i < n; i++){ for(j = 0; j < (n-1); j++){ a[i][j] = rand(); } } I try to generate random numbers, but they are the same... What should i do? Array declaration: int** a; int i; printf("Enter array size: "); scanf("%d", &n); a = (int**)calloc(n, sizeof(...

C, Cygwin, and compiling drand and srand

Hello, I have a C code which I am trying to compile in Cygwin and which contains both the drand() and srand() functions. I had Windows Vista with Cygwin installed and the code seemed to comile fine, but my computer broke and I had to get a new one. The new computer has Windows 7 64-bit version. I had a few issues downloading Cygwin b...

create a random sequence, skip to any part of the sequence

Hi everyone, In Linux. There is an srand() function, where you supply a seed and it will guarantee the same sequence of pseudorandom numbers in subsequent calls to the random() function afterwards. Lets say, I want to store this pseudo random sequence by remembering this seed value. Furthermore, let's say I want the 100 thousandth n...

rand () for c++ with variables...

int userHP = 100; int enemyHP = rand() % ((userHP - 50) - (userHP - 75)) + 1; okay, for some reason this doesnt seem to work right, im trying to get 50 -25 hp for enemys. also id rather it be a percentage... like int enemyHP = rand() % ((userHP / 50%) - (userHP / 75%)) + 1; but id like to stick with integers and not mess with flo...

rand() generating the same number – even with srand(time(NULL)) in my main!

So, I'm trying to create a random vector (think geometry, not an expandable array), and every time I call my random vector function I get the same x value, though y and z are different. int main () { srand ( (unsigned)time(NULL)); Vector<double> a; a.randvec(); cout << a << endl; return 0; } using the function //r...

Running Ruby as index.cgi, [1,3,5].shuffle always yield the same result

I do dump the value of RUBY_VERSION => 1.8.7 every time, the value of [1,3,5].shuffle is also [1,3,5] i have to add a srand(Time.now.to_i) or srand() in front of it to make it random... I thought srand is automatically called? but maybe not in a .cgi environment? if i use irb, and look at [1,3,5].shuffle, and exit, and re-enter irb, e...

Create a random even number between range

OK I need to create an even random number between 54 and 212 inclusive. The only catch is that it has to be done in a single statement. I have a class to generate random number within a range, but like I said, I want to do it in a single statement. I came up with this, but it's not working correctly. Any ideas? int main() { srand( ...

How to eliminate all sources of randomness so that program always gives identical answers?

I have C++ code that relies heavily on sampling (using rand()), but I want it to be reproducible. So in the beginning, I initialize srand() with a random seed and print that seed out. I want others to be able to run the same code again but initializing srand() with that same seed and get exactly the same answer as I did. But under what ...

Repeatable randomness in Ruby

I know I can "restart" my rand calls by calling srand with a seed, but surely this would affect future calls to rand by other library methods, including cryptographic methods? How can I repeat my randomness in certain places and still ensure that the rest of my code isn't affected? ...