I use random numbers for testing a lot, and I'm used to Excel, where you can get a lot of precision from '=RAND()'
It seems like the easiest way to replicate the Excel RAND function in Navision is with this code:
RANDOMIZE;
x := RANDOM(2147483647)/2147483647)
And, then this really isn't a true random number, but it's based on an algorithm that wouldn't pass really advanced randomness tests.
Is there something better in Navision than RANDOM? Or, do you know if future versions of Navision will support random numbers that are more random?
Comments
RandomDigit := CREATEGUID;
RandomDigit := DELCHR(RandomDigit,'=','{}-01');
RandomDigit := COPYSTR(RandomDigit,1,10);
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
Guaranteed to be random.
|To-Increase|
I tried this code:
With this I wanted to see if the last 2 digits (the milliseconds) can have all values and the count is always close to 1000. So this is not a bad system.
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
But, I did make a working prototype that gives pretty random numbers. It's pretty slow, and I would guess that it wouldn't pass too many heavy-duty randomness tests. Also, about half of the time this gives a number with far fewer decimal places than the max. I don't understand why my code does that.
But the numbers it generates do look random. Thanks for your ideas!
Microsoft Dynamics NAV User
I guess you could use some dll file that does only than, or even use Excel automation to generate a random number...
I use random numbers for testing, mostly. But random numbers are very helpful in a lot of situations. How would I learn how to hook Navision up to a DLL or Excel's automation?
Microsoft Dynamics NAV User
You can look at the Excel Buffer Table to see how to Interact with excel. Otherwise you could create a .NET dll or COM interface depending on the version.
-you need Excel installed (and probably it must be the correct version)
-It is probably slower than RANDOM because you have to connect to .NET or COM.
After all, why do you need a random no. in NAV. Only to do some tests and for that the C/AL RANDOM is good enough.
If you need random numbers in a scientific way, you
1) need some black box (don't remember the name) that generates really random numbers using quantum fluctuations (some companies have created such a thing)
2) you're really using the wrong language/development environment for that.
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!
However, I am very curious in what business process a random number is needed.
Is it like a random discount? If you're lucky you'll receive a 100% discount?
|To-Increase|
Get a dog instead. 8)
(And how is this ontopic? I seem to have missed the link between Shrodingers cat and randomness)
|To-Increase|
And, I agree, RANDOM is usually good enough. But, NAV is an extensible ERP system after all. And NAV's a top tier product, also.
From the NAV documentation, RANDOM returns "A pseudo-random number." I guess the thing that bugs me most about it, is that this is a reminder than Dynamics NAV is built on top of a super old, creaky programming language. Pretty much all modern programming languages have the ability to generate real random numbers, and have gotten rid of the pseudo-random algorithms.
Microsoft Dynamics NAV User
There are only two differences, as far as I can see:
#1: Most modern programming languages have the RANDOMIZE command built in. For example, Java uses as a seed the number of milliseconds since January 1, 1970. So, you don't have to initialize random numbers with a RANDOMIZE command in other languages.
#2: In Navision, you get a random number in only 4% of the total possible set of random numbers 1 and 2,147,483,647, for your first random number. Here's the math. There are 24*60*60*1000 (86,400,000) milliseconds in a day, so this is the maximum number of possible seed values for the random number. So the most unique random numbers that will be returned are 86,400,000/2,147,483,647, or 4.02%.
Or, in other words, the following code will never return 96% of all possible numbers between 1 and 2147483647: But, the second time you run the second line, it should give equal chances of getting every number.
So, are Navision random numbers really random? I guess the answer really is: they're random enough.
Microsoft Dynamics NAV User
Try to implement a simple Dice.
The first number is always an odd value (1, 3 or 5). :bug: :bug: :bug: :bug: :bug:
BTW: It's the same with all even values in the random function.
random(<even value>) returns an <odd value> at the first try.
Kind regards,
winfy
Thanks very much! This is great, simple sample code. I tested it a bunch of times and I never got an even number. I'm curious how you found this random deficiency in NAV.
Microsoft Dynamics NAV User
Park and Millers Minimal Standard Random Number Generator is simple, fast and in my opinion good enough for the most simulations in Navision.
Random2(VAR Seed : Integer;Number : Integer) : Integer
Someone post it here before.
I think it's good enough to generate a Startseed with it.
Randomize2(VAR Seed : Integer)
Maybe useful?
Literature: (Link)
Kind regards,
winfy
Nonetheless, here's my seeding solution, which is probably slow, but hopefully there's always enough schedule changes between multiple simultaneous calls to ensure different last loop times. I assume CREATEGUID uses a higher precision timer. I'll do testing with parallelism and post my findings.
What I wrote was something like that.
WHILE NOT UniqueValue DO BEGIN
"Tracking No":=GenerateTrackingNo;
IF NOT CheckDuplicateTracking("Tracking No") THEN
UniqueValue := TRUE;
END;
Where GenerateTrackingNo was the function which was generating unique number with Random NAV Keyword and CheckDuplicateTracking was the function where I was just cross validating that the generated tracking number doesn't exist in the Database.
P.S - Till 6 month it was OK but after then what I experienced was system had started generating duplicate tracking numbers hence added to cross verify with the existing one in the DB.
Blog - rockwithnav.wordpress.com/
Twitter - https://twitter.com/RockwithNav
Facebook - https://facebook.com/rockwithnav/
If you run within the same machine while there is still a time required do copy the data between one process and another.
Dynamics NAV, MS SQL Server, Wherescape RED;
PRINCE2 Practitioner - License GR657010572SG
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
CREATEGUID is probably calling .NET Guid.NewGuid so I looked up Microsoft's explanation of what's happening under that hood.
Remarks
This is a convenient static method that you can call to get a new Guid. The method wraps a call to the Windows CoCreateGuid function. The returned Guid is guaranteed to not equal Guid.Empty.
Remarks
The CoCreateGuid function calls the RPC function UuidCreate, which creates a GUID, a globally unique 128-bit integer. Use CoCreateGuid when you need an absolutely unique number that you will use as a persistent identifier in a distributed environment.To a very high degree of certainty, this function returns a unique value – no other invocation, on the same or any other system (networked or not), should return the same value.
CREATEGUID is practically immune if it's using this, which is very good.
No PM,please use the forum. || May the <SOLVED>-attribute be in your title!