0

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.

Rahbee Alvee
  • 1,924
  • 7
  • 26
  • 42

7 Answers7

4

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

Poindexter
  • 2,396
  • 16
  • 19
  • If you want better random numbers at the cost of performance you should use SecureRandom instead. – Peter D Nov 23 '09 at 15:16
3

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
  • 2,438
  • 23
  • 30
2

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.

Community
  • 1
  • 1
Ruben
  • 6,367
  • 1
  • 24
  • 35
2

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

Kevin
  • 31
  • 1
  • 1
    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 Nov 23 '09 at 14:53
2

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
  • 6,560
  • 13
  • 65
  • 123
1

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
  • 11,419
  • 5
  • 44
  • 69
1

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
  • 20,852
  • 4
  • 42
  • 54