TradingView
ImmortalFreedom
Apr 29, 2020 10:59 PM

Pseudo-Random Number Generators via Pine Script 

Description

These are my "Pseudo-Random Number Generators via Pine Script", having uniform distribution between 0 and 1. In my quest for sequential pseudo-random numbers in Pine, tinkering with mathematical chaos, I have come up with these three simple yet handy generators. I believe these may be useful in the future by developers of the Tradingview community.

NOTICE: Use it as you please, this is free code to implement into your new Pine projects. You don't have to ask for my permission to reuse these functions in your published scripts, simply because I have better things to do than answer requests for the reuse of these simplistic PRNG functions. I don't require accreditation for members to reuse these functions either. I simply don't want any credit. I only require that you respectfully put it to "good and proper use" complimenting the elaborate script you have in mind. I would however like to see some great ideas utilizing these when I randomly and accidentally come across them in the public library. I didn't know where to put this script, so I just tossed it into the "Trend Analysis" category.

The comments section below is solely just for commenting and other remarks, ideas, compliments, etc... regarding only this indicator, not others. When available time provides itself, I will consider your inquiries, thoughts, and concepts presented below in the comments section, should you have any questions or comments regarding this indicator. When my indicators achieve more prevalent use by TV members, I may implement more ideas when they present themselves as worthy additions. As always, "Like" it if you simply just like it with a proper thumbs up, and also return to my scripts list occasionally for additional postings. Have a profitable future everyone!
Comments
PineCoders
This publication will be featured in our "Best Scripts of The Month" selection. Thank you for your valuable contribution to the TradingView community, and congrats!
Parimal_S
Thank you.
StefanReich
Thanks. Why 3 functions though? Is one better than the others in any way or certain circumstances?
ImmortalFreedom
@StefanReich, @StefanReich, It was just a an exercise in random ways to generate random numbers in Pine. Pine formerly didn't have math.random(min, max, seed) like it does now. While two have greater computational expense, sprng() is the 'faster' calculation. It's good to have algorithms with diversity. Years later, I used sprng() to build my own matriculated screensaver from scratch. I built a Pine version, but it blows out the server resources and it's limited visually. I never completed it, once I realized Pine is not ideal for making screensavers... yet. I was having trouble with Math.random() consuming too much of the CPU core resources, and my screensaver FPS was deficient like a turtle on a hamster wheel. I had to build a FPS counter to see which PNRG was faster. Without my sprng(), my personal digitized rain project would have been a flop, but now it's better than any screensaver I ever used in the past 20ish years. It's VERY different from the original seen in the movie, superseding what Hollywood created in visual randomness and vivid color morphing techniques. I needed A LOT of random, but it had to be efficient. These PNRGs are ancient history, but they are still applicable I suppose. Pine's built-in math.random() is most likely a "better" random and you can just use it, no function importing as is needed with theses PNRGs. There is random and there is pseudo-random, each with it's own attributes. It all depends on what you truly seek for a goal.
StefanReich
@ImmortalFreedom, lol, I hadn't even seen that math.random is a thing now. Thanks for the explanation
ImmortalFreedom
@StefanReich, Many people requested it to be a built-in, so TV implemented it. They also included the ability to have maximum, and minimum limitations, so you can do {0.0,1.0}, +/1.0, or any other range you require.
slowcoconut
muahaha, I was going to whip up something like this for an indicator I made that accepts historical sampling points as input...so far I've been using famous number sequences such as pronic numbers, Ramanujan primes, 'magic' numbers...etc. but I wondered what would happen if I randomly generated a fresh sequence of integers to use as sampling points on each bar or after a certain number of bars...thanks!
pyro_sun
Thank you
blackcat1402
great work to have random numbers, pine is still lack a radom function to be perfect :D
allanster
Very gracious of you, thanks so much for sharing these!
More