Hey guys!
I'm trying to make a function that create a random between 0 and 1 from any number you type in. I tried many different ways but failed every time.. it seems so easy! I hope someone can help me with this.
Hey guys!
I'm trying to make a function that create a random between 0 and 1 from any number you type in. I tried many different ways but failed every time.. it seems so easy! I hope someone can help me with this.
At 10/28/13 05:05 PM, WolveGames wrote: Hey guys!
I'm trying to make a function that create a random between 0 and 1 from any number you type in. I tried many different ways but failed every time.. it seems so easy! I hope someone can help me with this.
If this isn't purely academic, and you just require a working random implementation then I suggest you use whatever implementation that your language provides. Sometimes the implementation will be a very simple linear function of the type next = (a * last + c) % max, sometimes the random value will be taken from a buffer the motherboard fills up with white noise when it boots, so the actual randomness of the result will vary, but will generally be good enough.
If you actually want to implement a random number generator by hand then know that most of the implementations will look like the formula I gave above (next = (a * last + c) % max) mostly because it's fast and good enough for mundane purposes. In this case the seed is the first value that you feed into last. If you want something more complex (e.g. a random number generator suitable for cryptographic purposes, then I can't help you, but this might be a good starting point)
You could fiddle with the Middle-Square algorithm to produce 0's or 1's.
Seed: 5422
(5422)^2
29398084
29398084
Output: 3980
Output/Seed
3980/5422
= 0.73404
Round.
1
thats both very helpful! I think I can code myself out of it now :)