dim as integer numarr(0 to 99)
dim as integer counter
randomize timer,3
for counter = 0 to 99
numarr(counter) = rnd * 10000
next
I want to find the largest value in the array. How?
A descending sort would probably work. I have tried several things without any success. In certain situations where I would use random numbers in other parts of code, I will use a zero-based array, or similar. The larger the multiplier for the random, the greater range the values will be.
Note: I searched here for quite a while and could not find any I could use. Perhaps my criteria was incorrect.
dim as integer numarr(0 to 99)
dim as integer counter
dim as integer maxnum=-1
randomize timer,3
for counter = 0 to 99
numarr(counter) = rnd * 10000
next
for counter = 0 to 99
if numarr(counter)>maxnum Then maxnum = numarr(counter)
next
print "The largest number is ";maxnum
Sleep
storm5510 wrote: ↑Feb 20, 2022 0:13I want to find the largest value in the array. How?
A descending sort would probably work.
Sorting is pointless. The fastest sort algorithm is O(n log n), which means you have to step through all n elements anyway, and more, on average. If you just loop through all the elements and look for the biggest, that is O(n), which is going to be faster by a factor of log n than sorting. This is a case where the simplest, brute-force solution is the fastest, as @jj2007's example demonstrates. The only change I would make to his code is to initialize maxnum to the smallest possible value, such as -9223372036854775808 for a 64-bit integer.
caseih wrote: ↑Feb 20, 2022 1:15The only change I would make to his code is to initialize maxnum to the smallest possible value, such as -9223372036854775808 for a 64-bit integer.
OP uses integers in numarr(counter) = rnd * 10000. There are no negative numbers in the array, therefore -1 is ok for 32- and 64-bit code.
Very true. But if a person wanted a generic solution that would always work, he should initialize to the smallest possible value for the size he's using.
dim as integer numarr(0 to 99)
dim as integer counter
randomize timer,3
for counter = 0 to 99
numarr(counter) = rnd * 10000
next
I want to find the largest value in the array. How?
A descending sort would probably work. I have tried several things without any success. In certain situations where I would use random numbers in other parts of code, I will use a zero-based array, or similar. The larger the multiplier for the random, the greater range the values will be.
Note: I searched here for quite a while and could not find any I could use. Perhaps my criteria was incorrect.
dim as integer numarr(99) ' Dim need only the max index of the array if the array have to start from 0, insted REDIM need from to (0 to 99)
dim as integer counter
dim as integer selected = 0 ' dimension
randomize timer,3
for counter = 0 to 99
numarr(counter) = rnd * 10000
next counter
for counter = 0 to 99 step 1
if numarr(counter) > numarr(selected) then selected = counter ' Compare the Array value storing the index with the biggest value
next counter
print "the Array with the biggest number are: " & selected
print "the Value in the array are: " & numarr(selected)
sleep ' Use sleep for windows compiling that prevend the close of the window before you read the output
As suggested by jj2007 and the tip of caseih it is sufficient to create a cycle that makes a comparison between the indices
for Dim if you dimension array from 0 ( 0 to num) is not needed
Oh yes good point. You can simply initialize maxnum to the first item of the array, and then only check the rest of the elements. That would work whether you're finding the largest or the smallest.
dim as integer numarr(99) ' Dim need only the max index of the array if the array have to start from 0, insted REDIM need from to (0 to 99)
dim as integer counter
dim as integer selected = 0 ' dimension
randomize timer,3
for counter = 0 to 99
numarr(counter) = rnd * 10000
next counter
for counter = 0 to 99 step 1
if numarr(counter) > numarr(selected) then selected = counter ' Compare the Array value storing the index with the biggest value
next counter
print "the Array with the biggest number are: " & selected
print "the Value in the array are: " & numarr(selected)
sleep ' Use sleep for windows compiling that prevent the close of the window before you read the output
I actually tried the above and it works very well, and far simpler than anything I tried.
The purpose for all this is to see how random the FB random number generator actually is. It is very random. I increased the number of array elements to 120, 0 to 119. I had to allow for a return value of zero. It's just easier to use 0 instead of 1. About 20 years ago, MS posted something which indicated their RNG's followed some type of pattern in their programming platforms. Where this was, I do not remember. It's been too long. They didn't offer a workaround as I recall.
I actually tried the above and it works very well, and far simpler than anything I tried.
The purpose for all this is to see how random the FB random number generator actually is. It is very random. I increased the number of array elements to 120, 0 to 119. I had to allow for a return value of zero. It's just easier to use 0 instead of 1. About 20 years ago, MS posted something which indicated their RNG's followed some type of pattern in their programming platforms. Where this was, I do not remember. It's been too long. They didn't offer a workaround as I recall.
The random number generator actually does not generate any random number but it is just an algorithm that considers the system clock and the CPU clock speed and something else that I don't remember, it is the same algorithm used by Microsoft for 20 years in Visual C ++, and Linux GCC, in any case the random number generator only used to create unique values that can be used to name a file, create random passwords, save random data or chosen by a VI (NPG) of a video game etc.
Personally, excluding random passwords, for the rest I prefer to use sequential data, perhaps inserting the date and time of storage.
storm5510 wrote:The purpose for all this is to see how random the FB random number generator actually is. It is very random.
For testing the quality of randomness the best method is to use PractRand. The default FB generator, Mersenne Twister, is the best of the bunch, but it fails the latest version of PractRand at 256GB. That is still good, but the generators developed in the last five years or so pass PractRand to at least 16TB and have a much faster throughput. They have an excellent distribution uniformity and some do not require a 'warm up'. Mersenne Twister takes a long time to 'warm up'. Very few commercial enterprises, if any, opt to use Mersenne Twister nowadays, and I have seen several blogs which discourage its use. It was developed in 1997 and is now past its 'sell by date'.