QB64 Phoenix Edition
Getting a random number wihout RND. - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Programs (https://qb64phoenix.com/forum/forumdisplay.php?fid=7)
+---- Thread: Getting a random number wihout RND. (/showthread.php?tid=2052)

Pages: 1 2 3


RE: Getting a random number wihout RND. - mnrvovrfc - 10-02-2023

Some of you have a hard time understanding alternative ways of doing things.

Try programming for Timex Sinclair and having to deal with only 64k values considered random numbers, and the "seed" only starts at a different point of the same table. There might have been a bit of black magic involved on that computer with 1024 bytes of RAM.


RE: Getting a random number wihout RND. - James D Jarvis - 10-02-2023

I remember having to write custom random # generators back in college because the standard library random number generator failed at being random enough for any serious mathematical programming.   Of course I just remember doing it, not what we did.


RE: Getting a random number wihout RND. - SpriggsySpriggs - 10-02-2023

One could also use GUIDs/UUIDs for seeds.

In Linux:
`cat /proc/sys/kernel/random/uuid` or `uuidgen`

In Windows with PowerShell installed:
`PowerShell -NoProfile [guid]::NewGuid().ToString()`

Both ways would require you to output to a file or use pipecom to capture the stdout.
You could also use the WinAPI to generate version 1 (sequential and based on MAC address) and version 4 (random) UUIDs.
The above terminal command options generate version 4 UUIDs, which are truly unique and don't reveal your MAC address.

You could also use the new HTTP options in QB64PE to do a simple GET request with the uuidtools REST API. It can generate versions 3 and 5 and create the UUIDs based on a hash of a provided string. UUIDs made this way are NOT random and can be created repeatedly. Can be handy to verify input against a stored UUID.

EDIT - Using the uuidtools REST API:
Code: (Select All)
Function UUIDVer3$ (mode As Integer, StringToHash As String)
    Dim As String UUID, URL, location
    'Dim As GUID GUID
    Dim As Long status
    location = "/api/generate/v3/namespace/"
    Select Case mode
        Case 1
            location = location + "ns:dns/name/"
        Case 2
            location = location + "ns:url/name/"
        Case 3
            location = location + "ns:oid/name/"
        Case 4
            location = location + "ns:x500/name/"
    End Select
    Dim As Long client: client = _OpenClient("HTTP:" + "https://www.uuidtools.com" + location + StringToHash)
    If client Then
        If _StatusCode(client) = 200 Then
            Dim As String buf, content
            While Not EOF(client)
                Get client, , buf
                content = content + buf
            Wend
            Close client
            UUID = UCase$(Mid$(_Trim$(content), 3, 36))
        End If
    End If
    'StringToUUID UUID, GUID
    'GUIDHash = UuidHash(GUID, status)
    UUIDVer3 = UUID
End Function

Function UUIDVer5$ (mode As Integer, StringToHash As String)
    Dim As String UUID, URL, location
    'Dim As GUID GUID
    Dim As Long status
    location = "/api/generate/v5/namespace/"
    Select Case mode
        Case 1
            location = location + "ns:dns/name/"
        Case 2
            location = location + "ns:url/name/"
        Case 3
            location = location + "ns:oid/name/"
        Case 4
            location = location + "ns:x500/name/"
    End Select
    Dim As Long client: client = _OpenClient("HTTP:" + "https://www.uuidtools.com" + location + StringToHash)
    If client Then
        If _StatusCode(client) = 200 Then
            Dim As String buf, content
            While Not EOF(client)
                Get client, , buf
                content = content + buf
            Wend
            Close client
            UUID = UCase$(Mid$(_Trim$(content), 3, 36))
        End If
    End If
    'StringToUUID UUID, GUID
    'GUIDHash = UuidHash(GUID, status)
    UUIDVer5 = UUID
End Function
   


RE: Getting a random number wihout RND. - eoredson - 10-02-2023

(10-02-2023, 03:04 PM)euklides Wrote: randomize timer ???

Yes. According to the Wiki using Randomize Timer is standard.

https://qb64phoenix.com/qb64wiki/index.php/RANDOMIZE


RE: Getting a random number wihout RND. - mnrvovrfc - 10-03-2023

Linux only.
Code: (Select All)
$CONSOLE:ONLY
DIM ch(1 TO 10) AS _UNSIGNED _BYTE
DIM a$, ff AS LONG, i AS INTEGER

'this should be an "unique" filename which contents get erased after use.
a$ = "/tmp/c10headevrancat"
'the "file" could go on forever, so get only the first 10 characters.
'could replace with "urandom" but there was none on the system I tried.
SHELL "cat /dev/random | head -c 10 > " + a$

ff = FREEFILE
OPEN a$ FOR BINARY AS ff
FOR i = 1 TO 10
GET #ff, , ch(i)
NEXT
CLOSE ff
'some terminals try to brute-force translate UTF-8 for high-bit characters
' so have to do something about them and about the "control" characters.
FOR i = 1 TO 10
IF ch(i) < 32 OR ch(i) > 126 THEN
PRINT " ("; str(ch(i)); ") "
ELSE
PRINT CHR$(ch(i));
END IF
NEXT
PRINT
SYSTEM



RE: Getting a random number wihout RND. - Pete - 07-17-2024

True randomness would mean if you ran your program, recorded the results then hopped in your Delorian to go back in time, the results would be different. Go figure.

Pete Huh


RE: Getting a random number wihout RND. - vince - 07-17-2024

looks like Pete is bored today

but the only correct answer is https://en.wikipedia.org/wiki/Rule_30#Random_number_generation

either that or stick a wire between the undecoded front of your wifi reciever and your mircophone input jack in order to properly quantum detangle that bit


RE: Getting a random number wihout RND. - Pete - 07-17-2024

Oh rats, C++, and I'm not bored enough to want to convert it to QB64, just so it can be compiled back to C++, and for what? To generate a SCREEN 0 Christmas tree with random ornaments? Like my old friend Resident Alien always says... "This is bullshit!"

Pete Big Grin

- Infinity doesn't exist; it's just the 'out to lunch' sign scientists put up when they get tired of counting.


RE: Getting a random number wihout RND. - Pete - 07-17-2024

I liked this part of an article I've been reading...

" As an aside, it turns out that the absolute randomness comes from the fact that every result of every interaction is expressed in parallel universes (you can’t predict two or more mutually exclusive, yet simultaneous results).  “Parallel universes” are not nearly as exciting as they sound.  Things are defined to be in different universes if they can’t coexist or interact.  For example: in the double slit experiment a single photon goes through two slits.  These two versions of the same photon exist in different universes from their own points of view (since they are mutually exclusive), but they are in the same universe from our perspective (since we can’t tell which slit they went through, and probably don’t care).  Don’t worry about it too much all at once.  You gotta pace your swearing."

Big Grin Big Grin Big Grin 

This post isn't really here, but then neither are you... and that's why you can read it!

Pete


RE: Getting a random number wihout RND. - vince - 07-17-2024

Sold enough quantum spinal alignment bracelets at your office, Pete?

I just noticed Terry mentioned the radio tuned to a random station idea, +1 for that.  Speaking of hardware RNGs, they also sell them as these giant PCI cards and the like, some expensive ones with all then quantum nonsense thrown in for good measure.  From what I remember, linux drives two random number generators /dev/random and /dev/urandom, the latter injecting your PC hardware noise into the mix.  I guess most devices like CPUs, GPUs will have a temperature sensor and if you grab a value well below the noise floor, ie the 5th decimal place or something (per spec), I imagine it's enough black body radiation to deter cheating on your QB64PE tic tac toe mod