Finding the largest number.

New to FreeBASIC? Post your questions here.
Post Reply
storm5510
Posts: 59
Joined: Nov 28, 2019 15:47

Finding the largest number.

Post by storm5510 »

I have an array of 100 numeric elements:

Code: Select all

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.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Finding the largest number.

Post by jj2007 »

Code: Select all

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
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Finding the largest number.

Post by caseih »

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.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Finding the largest number.

Post by jj2007 »

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.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Finding the largest number.

Post by caseih »

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.
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Finding the largest number.

Post by exagonx »

storm5510 wrote: Feb 20, 2022 0:13 I have an array of 100 numeric elements:

Code: Select all

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.

Code: Select all


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
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Finding the largest number.

Post by caseih »

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.
storm5510
Posts: 59
Joined: Nov 28, 2019 15:47

Re: Finding the largest number.

Post by storm5510 »

exagonx wrote: Feb 20, 2022 20:31

Code: Select all


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.
exagonx
Posts: 315
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: Finding the largest number.

Post by exagonx »

storm5510 wrote: Feb 21, 2022 16:39

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.
deltarho[1859]
Posts: 4308
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Finding the largest number.

Post by deltarho[1859] »

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'. :)
Post Reply