Author Topic: fun with randomize  (Read 7446 times)

0 Members and 1 Guest are viewing this topic.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
fun with randomize
« on: November 13, 2021, 05:23:13 am »
I did a search, did not see anyone approach randomize from this angle.
I saw that randomize needs a seed (of course)
Most people just use "timer" or if your goofy like me "-timer"
But reading about timer, I saw that this only  returns the number of seconds past the previous midnite

Interesting..

That means the next day, it is quite possible to have the same seed (ish).

So just for fun, came up with something different. It is based on the clock, and no repeats.

Opinions?

Code: QB64: [Select]
  1. Seed = stripTime
  2. Randomize(Seed)
  3. Function stripTime
  4.     Dim t As Long
  5.     Dim d As Long
  6.     Dim s As String
  7.  
  8.     '// strip date
  9.     s = Left$(Date$, 2) + Mid$(Date$, 4, 2) + Right$(Date$, 4)
  10.     d = Val(s)
  11.     '// strip time
  12.     s = Left$(Time$, 2) + Mid$(Time$, 4, 2) + Right$(Time$, 2)
  13.     t = Val(s)
  14.     stripTime = d + t
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: fun with randomize
« Reply #1 on: November 13, 2021, 05:27:16 am »
line 14 you can do all kinds of math stuff to make it more random.  Except might be a good idea to change seed to _integer64.
I'm not sure how big randomize can take, but it hasn't thrown a fit yet. done d * t and does fine.
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: fun with randomize
« Reply #2 on: November 13, 2021, 06:03:51 am »
I may be wrong, most times I am, but I'm confused.
"Timer" is generally used as a "seed" but is a number made up of passed seconds...

I can understand that 'stripdate' d is the numeric value of of the 8 digits making up the "date" and the 'striptime' t is the numeric value of the 6 digits making up the "time" and the variable stripTime is the concatenation of both d and t... but the only part of the new 'seed' that changed is also seconds.

I fail to see the difference. Both Timer and stripTime produce results by counting seconds. Am I making any sense?
Logic is the beginning of wisdom.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: fun with randomize
« Reply #3 on: November 13, 2021, 09:51:42 am »
timer used for a seed can be replicated the next day

of course I am splitting hairs

if you play at 2:00pm today example i think 50400 is 2pm
then play at 2:00pm tomorrow, guess what, the timer will be at 50400

there is a (low)chance the seed will be identical. Remember: it resets and  returns the number of seconds past the previous midnite. Not many programs have good ranomizer - and timer makes a poor one.

stripTime will never be the same. And you can use addition, multiplication, or any fancy math equation for d and t. It only uses the date and time as a base. Timer is a consistent value that CAN repeat based on time of day.

Again, just for fun.
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline doppler

  • Forum Regular
  • Posts: 241
    • View Profile
Re: fun with randomize
« Reply #4 on: November 13, 2021, 10:15:02 am »
Random does not exist in this reality.  Or in this quantum universe.
The holy grail of mathematics is to find a way to get true random.  Finding a true random system may involve IMHO the math equation PI.  But true PI does not exist either (we only get a really long number of it.).

The closest to random any one has gotten, involved a radioactive source and a atomic clock.  Counting particles and time time between detection's.  Even the shower of neutrons that past by all the time, was shown to be not random.

Good luck.  A noble prize awaits for you if you find it.


Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: fun with randomize
« Reply #5 on: November 13, 2021, 10:40:03 am »
You haven't seen my ex! :-)

however, this is very true..

I just thought I would share the bump up from the hum-drum randomize(timer)

I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: fun with randomize
« Reply #6 on: November 13, 2021, 07:04:21 pm »
Understood. Thank you for the explanation. Much appreciated.

'Random' does not exist, 'Pi', 'Neutrons'... It was SO much simpler using a Coin Toss... My brain hurts... lol
Logic is the beginning of wisdom.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: fun with randomize
« Reply #7 on: November 13, 2021, 09:21:04 pm »
@xra7en

Interesting idea but d + t as numbers are NOT as unique as d + t are concatenated as strings! So johnno56 had it right  sorta...
Quote
and the variable stripTime is the concatenation of both d and t
when the code showed it to be d+t as number!

eg
10 + 15 = 15 + 10 = 5 + 20 = 1 + 24....

1015 <> 1510 <> 520 <> 124
« Last Edit: November 13, 2021, 09:46:52 pm by bplus »

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: fun with randomize
« Reply #8 on: November 13, 2021, 09:55:49 pm »
@xra7en

Interesting idea but d + t as numbers are NOT as unique as d + t are concatenated as strings! So johnno56 had it right  sorta...when the code showed it to be d+t as number!

eg
10 + 15 = 15 + 10 = 5 + 20 = 1 + 24....

1015 <> 1510 <> 520 <> 124

well the purpose actually was to make it so the "seed" cannot be duplicated like timer can.  And d + T is arbitrary as you could also do d * T, d / t etc...
the d and t was just used to make it so the numbers are unique.
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: fun with randomize
« Reply #9 on: November 13, 2021, 10:09:24 pm »
well the purpose actually was to make it so the "seed" cannot be duplicated like timer can.  And d + T is arbitrary as you could also do d * T, d / t etc...
the d and t was just used to make it so the numbers are unique.

That's what I am saying a date string and a time string concatenated is more unique that numbers added together.
An 8 digit number plus a 6 digit number is at most a 9 digit number, 1 in 10^9 -1

An 8 digit date and a 6 digit time string concatenated is a 14 digit number, 1 in 10^14 -1

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: fun with randomize
« Reply #10 on: November 13, 2021, 10:33:11 pm »
That's what I am saying a date string and a time string concatenated is more unique that numbers added together.
An 8 digit number plus a 6 digit number is at most a 9 digit number, 1 in 10^9 -1

An 8 digit date and a 6 digit time string concatenated is a 14 digit number, 1 in 10^14 -1

O sorry, misread that.
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: fun with randomize
« Reply #11 on: November 14, 2021, 12:55:56 am »
I do not mean to harp on about this but... the 14 digit number is made of MMDDYYYYHHMMSS, right?

Wikipedia states: "A pseudorandom number generator's number sequence is completely determined by the seed: thus, if a pseudorandom number generator is reinitialized with the same seed, it will produce the same sequence of numbers."

This is part of the point that I am making... When the seed is generated, in this case, only SS changes within the first minute. Which means an opportunity exists for the same random number to be generated... albeit small... but still possible.  If 'all' 14 digits, that make up the seed, are different each time a random number is requested, the occurrence of the same number produced would be much smaller.

My suggestion would be, if randomize(seed) was going to be used, then place it within the loop, changing the seed each time, when creating the random number, instead of placing at the beginning of the program. This may increase CPU usage but it would guarantee that the seed would be re-initialized before producing the random number, thereby produce a number less likely to repeat...  I still think a coin toss is simpler... lol

Just a thought...
Logic is the beginning of wisdom.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: fun with randomize
« Reply #12 on: November 14, 2021, 01:21:49 am »
Someone change my mind about

Code: QB64: [Select]

  [ You are not allowed to view this attachment ]  

Code: [Select]
─────█████████████████████████──
──────▀█▄▀▄▀████▀──▀█▄▀▄▀████▀──
────────▀█▄█▄█▀──────▀█▄█▄█▀────
You're not done when it works, you're done when it's right.

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: fun with randomize
« Reply #13 on: November 14, 2021, 03:41:48 am »
Interesting. Could be a catch-22...

First example: Rnd in practice, will produce the same result each time it run.
Second example: Randomize Rnd should produce the same number each time it is run.

First example: let's say that Rnd produces 0.724536. The next time it runs it will produce the same number because Rnd has no seed.

Second example: let's say that  Randomize Rnd produces 0.1853487. The next time it runs will produce the same number. Randomize expects a seed number. Rnd requires a seed. Around and around we go. Because Randomize is not getting a seed number the result should always be the same.

Well this is fun... Noodle bending but fun... Where's my coin?
Logic is the beginning of wisdom.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: fun with randomize
« Reply #14 on: November 14, 2021, 10:10:45 am »
@johnno56
exactly!
I took it at an angle not as complex

the timer has a high chance of producing repeat seeds. I mean there are only 86,400 seconds (units/ticks etc) in a 24hr period to to work with, then it repeats. In computer language that is just spit in the ocean.

for me - I wanted another set of numbers that don't repeat. time/date will never repeat. It is always incrementing, so in the timer example. the NEXT DAY there is zero chance it will reproduce a same number as the day before. THERE IS HOWEVER a chance that one of the 86,400 numbers will repeat.

I am curious about the coin toss - its binary.  if 1 then this, else if 0 then that.  how would you produce a value say, less than 62% :-)


ps
Wiki is written by RANDOM people from RANDOM places, so logic would suggest their answers are RANDOM as well :P Of course that is a RANDOM statement as well LOL
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!