00:00
00:00
Newgrounds Background Image Theme

GodsPurpose just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

random from seed

503 Views | 3 Replies
New Topic Respond to this Topic

random from seed 2013-10-28 17:05:38


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.


.

Response to random from seed 2013-10-28 17:21:26


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)

Response to random from seed 2013-10-28 18:39:49


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


To protect the world from devastation.

kidd25 is the best Newgrounder ever.

BBS Signature

Response to random from seed 2013-10-28 19:48:51


thats both very helpful! I think I can code myself out of it now :)


.