6

In JavaScript, is it possible to generate a random number from another number?

I'm trying to implement a predictable random number generator for one of my fractal terrain generators. I already know that it's possible to generate a random number using Math.random(), but I want to create a random number generator that produces exactly one output for every input. (For example, predictableRandomGenerator(1) would always produce the same result, which would not necessarily be the same as the input.)

So is it possible to generate a random number from another number, where the output is always the same for each input?

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • 5
    +1 for open-ended question :) – Bill May 25 '13 at 03:59
  • [I did a Google search for this question](https://www.google.com/#output=search&sclient=psy-ab&q=generate+a+random+number+from+another+number&oq=generate+a+random+number+from+another+number&gs_l=hp.3...1535.7376.1.7602.44.30.0.13.13.0.320.2958.20j8j1j1.30.0...0.0.0..1c.1.12.hp.SBAwn8WM_NM&psj=1&bav=on.2,or.r_cp.r_qf.&bvm=bv.47008514,d.dmg&fp=5f2a042325b2882d&biw=1366&bih=639), and I didn't find any relevant results, so I decided to post it here. – Anderson Green May 25 '13 at 04:00
  • Related: [What integer hash function are good that accepts an integer hash key?](http://stackoverflow.com/questions/664014/what-integer-hash-function-are-good-that-accepts-an-integer-hash-key) – Blender May 25 '13 at 04:02
  • just curious -- what are some use cases for such need? – Bill May 25 '13 at 04:06
  • What's your range for the input and what's your range for the output? Also do you want integers? I can think of a few things but I'm not sure what the ranges are... (for example `predictableRandomGenerator(n)` can return the nth digit of pi or the nth prime) – Albert Renshaw May 25 '13 at 04:08
  • @AlbertRenshaw: Both of those are really slow. – Blender May 25 '13 at 04:09
  • 2
    Can you seed `Math.random(input)`? Then you should always get the same sequence of "random" numbers (per seed). And you can just take the first one. But be aware that this is not unique and `Math.random(x)` and `Math.random(y)` may produce the same first "random" number. – Corak May 25 '13 at 04:09
  • 1
    Okay, apparently you can't seed the Javascript `Math.random()`... but maybe this'll help: http://stackoverflow.com/questions/424292/how-to-create-my-own-javascript-random-number-generator-that-i-can-also-set-the – Corak May 25 '13 at 04:11
  • You could create a VERY erratic tangent function and then use your nth value as the radians and get the resulting value from the function! – Albert Renshaw May 25 '13 at 04:12
  • Are you asking for an algorithm for a pseudo-random number generator or are you asking for a trap-door function? In the former case you should use the linear congruential pseudorandom number generator algorithm. In the latter case you should use a hash function like SHA-256. – Aadit M Shah May 25 '13 at 04:25
  • i think this would help http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html – Parthik Gosar May 25 '13 at 06:48
  • 1
    More or less a duplicate of this: http://stackoverflow.com/questions/521295/javascript-random-seeds – Atle May 25 '13 at 11:33
  • 1
    Another duplicate: http://stackoverflow.com/questions/664014/what-integer-hash-function-are-good-that-accepts-an-integer-hash-key – Thomas Mueller Jan 20 '16 at 07:39

5 Answers5

6

You can use a checksum generator such as MD5 or SHA-1 to generate a single pseudo-random output for every input. SHA-1 will generate a random number from each string that is entered as input, and each output will produce exactly one input. (It's likely that any other checksum generator would be suitable for thus purpose as well, since checksum generators produce exactly one output for each input that is entered).

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • The big caveat here is that checksum algorithms like MD5 and SHA-1 are essentially designed to be *slow*. What you really want is a seeded pseudorandom number generation algorithm. The keyword here is seeded, which is the technical term for that "starting number" to which you refer. – pauljz May 25 '13 at 04:55
  • 2
    Please do not use StackOverflow as your personal blog. This site is for asking real programming questions, not for sharing your inventions with the world. – georg May 25 '13 at 10:10
  • @thg435 Self-answering questions [is actually encouraged on Stack Overflow](http://meta.stackexchange.com/a/12519/177227). – Anderson Green May 25 '13 at 18:07
  • 4
    @AndersonGreen: Yes, self-answering a question is ok, provided that you've got an actual question about a real programming problem. Not some random idea you think we need to know about. – georg May 25 '13 at 20:28
  • @thg435 This question discusses a real programming problem. It asks how to implement a random number generator with output that can easily be reproduced. – Anderson Green May 25 '13 at 23:18
  • I agree with @thg435, you should only post a question you genuinely don't know the answer to. If you discover the answer after posting the question (what the self-answering link is referring to), posting that answer is great. Clearly this is not the case here as your answer is time stamped at exactly the same time as your question. – WetlabStudent Mar 10 '14 at 04:55
  • @MHH Whenever you write a question, an "Answer your own question" button is displayed. So it's clear that self-answering questions (even at the same time as posting them) is encouraged, as long as the question and its answer are both useful. – Anderson Green Mar 10 '14 at 05:26
4

Yes, it is possible. However you'll need to write your own pseudo-random number generator.

See, computers can't really generate random numbers. However you can use an algorithm which creates a sequence of numbers which appears to be random.

This algorithm is usually given a seed and each seed leads to a different sequence of random numbers generated by the algorithm.

The most common algorithm is the linear congruential pseudorandom number generator, as defined by D. H. Lehmer and described by Donald E. Knuth in The Art of Computer Programming, Volume 2: Seminumerical Algorithms, section 3.2.1.

For more details refer to the following thread: Predict the Seed of Javascript's Math.random

Community
  • 1
  • 1
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
3

Sure, how about the identity function:

function getMappedRandom(random){ return random; } 

I'm not sure why you want this transformation, but in terms of randomness it does not necessarily make it better.

Random Number Generator

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
dchhetri
  • 6,926
  • 4
  • 43
  • 56
  • This will simply return the input as output. Is it still considered pseudo-random? – Anderson Green May 25 '13 at 04:42
  • 1
    @AndersonGreen: btw, if you're honest, you should accept this one. This is the most accurate and correct answer to your question as posed. – georg May 25 '13 at 20:30
  • @thg435 Technically, this would be considered a type of random number generator, but a pseudo-random number generator that returned its output as input might would be much less "random" than most other random number generators, as judged by [statistical randomness tests](http://en.wikipedia.org/wiki/Tests_of_randomness). Perhaps the original wording of my question was misleading, since it seemed to imply that I wanted the random number generator to produce its input as output. – Anderson Green May 25 '13 at 23:20
  • @AndersonGreen: the question was how to generate a random number from another number, to which the answer `return 9` is totally correct. The property of being random doesn't apply to individual objects, therefore one number is just as random as another. It's only a sequence of numbers that can be random or not. – georg May 25 '13 at 23:49
  • Uh, how do you think most PRNGs work? The generated value is determined from the previous value. – jamesdlin May 25 '13 at 23:52
  • 1
    @thg435, the comic strip wasn't a part of the original answer. That was my addition. I thought it was appropriate seeing how the original answerer used an identity function. =) – Aadit M Shah May 26 '13 at 03:47
  • @AaditMShah: return self, return 9 or http://xkcd.com/221/ are equally good here. You can even choose randomly between them ;) – georg May 26 '13 at 08:54
2

I believe what you need is called one-way hash function. try hex_md5() or hex_sha1().

Fazle Rabbi
  • 902
  • 7
  • 4
0

If you need a PRNG for a terrain generator, then I'm assuming you need a seeded random number generator that reproduces the sequence of pseudo-random numbers for a given seed; Such that each seed creates a separate, distinct terrain that can be reconstructed later by providing the same seed.

This might work for you:

http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html http://davidbau.com/encode/seedrandom.js

jongo45
  • 3,020
  • 2
  • 16
  • 12