Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used following function to generate array of random numbers ,when i set seed
C++
srand(0)
this program generate exact same array of integers every time, i.e.
C++
38 39 118 5 23 21 45 29
Now i want to know seed of srand() by feding same array.

What I have tried:

i used these codes to generate array
C++
#include<iostream>
#include<conio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main(void) {
	int i;
    srand(0);
     for (i=0; i<8;i++)
	{
        int x = rand()%128;     //between 0 and 128
        cout<<x<<" ";
    }
}
Posted
Updated 20-Nov-16 20:27pm
Comments
Mehdi Gholam 21-Nov-16 1:47am    
It's computationally unpractical.
Peter_in_2780 21-Nov-16 2:01am    
Depends on the PRNG. A mixed congruential, for example, can be run "backwards". But OPs use of %128 is not only bad practice but would make this more difficult.
Mehdi Gholam 21-Nov-16 2:07am    
You need to compute the rainbow table of seeds and output numbers (which is very big) and will only work for starting bytes and would fail for "middle bytes" i.e. non starting sequences.

1 solution

Forget it. It's not at all practical. The seed value is an unsigned integer - which means a 32 bit number these days - so you have a possible 2^32 different array combinations you would need to "check back" to find: 4,294,967,296 possible values. And the main problem is that there are going to be a huge number of these that are the same - or at least start off the same - and you may need to get to a significant number of random number elements before you can eliminate some of them.

This is not a practical approach to anything. I know: I tried to use it as a "super compression" technique many, many years ago when I was just starting and eventually gave up when I realised just how long it was going to take to try and find a random number sequence that "generated" the byte stream I wanted to compress! :laugh:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900