tags:

views:

175

answers:

7

I need a random number generation algorithm that generates a random number for a specific input. But it will generate the same number every time it gets the same input. If this kind of algorithm available in the internet or i have to build one. If exists and any one knows that please let me know. (c, c++ , java, c# or any pseudo code will help much)

Thanks in advance.

+4  A: 

You may want to look at the built in Java class Random. The description fits what you want.

Poindexter
Thanks. I up voted you.
Mr. Flint
If you want better random numbers at the cost of performance you should use SecureRandom instead.
Peter D
+1  A: 

Some code like this should work for you:

MIN_VALUE + ((MAX_VALUE - MIN_VALUE +1) * RANDOM_INPUT / (MAX_VALUE + 1))
  • MIN_VALUE - Lower Bound
  • MAX_VALUE - Upper Bound
  • RANDOM_INPUT - Input Number
Bobby
Thanks. I up voted you.
Mr. Flint
+3  A: 

Usually the standard implementation of random number generator depends on seed value. You can use standard random with seed value set to some hash function of your input.

C# example:

string input = "Foo";
Random rnd = new Random(input.GetHashCode());
int random = rnd.Next();
Alex
Thanks. I up voted you. And choose your solution, it is easy enough :)
Mr. Flint
In fact if uoy need generate just one random number (not sequence) for each output than you can use GetHashCode() function instead of random.
Alex
Integer implementation of GetHashCode return the same int, so not really random.
Wilhelm
+2  A: 

I would use a hash function like SHA or MD5, this will generate the same output for a given input every time.

An example to generate a hash in java is here.

Ruben
Thanks. I up voted you.
Mr. Flint
+1  A: 

The Mersenne Twister algorithm is a good predictable random number generator. There are implementations in most languages.

Kevin
Mersenne Twister is far too complex for this requirement. MT has a high degree of recurrence which means it looks back at multiple samples in the history of outputs. Mersenne Twister 19937 for example (which is the usual MT) has a recurrence of 624, meaning that the state buffer, the number of previous samples saved, is 624 entries.In this case he just wants to generate a single sample from a single seed, so something far simpler with a recurrence of 1 is more more appropriate.
Tom
Thanks. I up voted you.
Mr. Flint
+1  A: 

How about..

public int getRandonNumber()
{
   // decided by a roll of a dice. Can't get fairer than that!
   return 4;
}

Or did you want a random number each time?

:-)

jeff porter
That's brilliant!
Tom
Thanks. I up voted you.
Mr. Flint
Hey, don't forget the source of this, please: http://xkcd.com/221/
Bobby
+1  A: 

All pseudo-random number generators (which is what most RNGs on computers are) will generate the same sequence of numbers from a starting input, the seed. So you can use whatever RNG is available in your programming language of choice.

Given that you want one sample from a given seed, I'd steer clear of Mersenne Twister and other complex RNGs that have good statistical properties since you don't need it. You could use a simple LCG, or you could use a hash function like MD5. One problem with LCG is that often for a small seed the next value is always in the same region since the modulo doesn't apply, so if your input value is typically small I'd use MD5 for example.

Tom
Thanks. I up voted you.
Mr. Flint