It's a kind of magic!

wonder

Expert
Licensed User
Longtime User
B4X:
Dim s As String
RndSeed(0x141EC22)
For i = 1 To 4
    s = s & Chr(Rnd(48, 123))
Next
Log(s)

This one's for @Erel! :D
 

wonder

Expert
Licensed User
Longtime User
It's magic, ahahah!! :D

I'll post the B4J source code once I get home.

EDIT: See post #9 for more elegant and optimized code!

Meanwhile here's the C version:
B4X:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_INT 2147483647
typedef enum { false, true } bool;

int rndChar()
{
    return 97 + (rand() % 26);
}

int main()
{
    bool   found = false;
    char   *key  = "erel";
    size_t len   = strlen(key);

    unsigned int i, s;
    for (s = 0; s < MAX_INT; ++s) {
        srand(s);
        for (i = 0; i < len; ++i) {
            found = false;
            if (rndChar() != key[i]) { break; }     
            found = true;
        }
        if (found) return s;
    } 
    return -1;
}

So far, I can find any string up to 6 characters without much hassle (~2 mins).
My next goal is to have it running on the GPU. Let's see what 1152 CUDA cores can do... ;)
 
Last edited:

ilan

Expert
Licensed User
Longtime User
:)

How did you find it?

maybe like this:

B4X:
    Dim seed As Long = -1
    Dim s As String
    Do Until s = "ilan"
        s = ""
        seed = seed + 1
        RndSeed(seed)
        For i = 1 To 4
            s = s & Chr(Rnd(48, 123))
        Next
    Loop
    Log(seed)

my seed is: 56205181 :p
 

wonder

Expert
Licensed User
Longtime User
Yep, pretty much! Almost the same! :)
B4X:
Dim found As Boolean
Dim guess As Char
Dim key  = "Erel" As String
Dim seed = -1     As Long
Do Until found
    found = True
    seed  = seed + 1
    RndSeed(seed)
    For i = 0 To (key.Length - 1)
        If Chr(Rnd(48, 123)) <> key.CharAt(i) Then
            found = False
            Exit
        End If
    Next     
Loop
Log(seed)


@ilan, as soon as a single character fails, there's no need to keep concatenating anything else to the string. Therefore:
B4X:
If Chr(Rnd(48, 123)) <> key.CharAt(i) Then
    found = False
    Exit
End If
 

wonder

Expert
Licensed User
Longtime User
This is the most elegant (in a certain style) and optimized I could come up with:
B4X:
Dim t               As Long    'Timestamp / completion time
Dim s = -1          As Long    'Seed
Dim i =  0          As Int     'Iterator
Dim k = "Erel"      As String  'Key
Dim l =  k.Length   As Int     'Key length
Dim c(l)            As Int     'Numeric key (array)
For p = 0 To l - 1                   
    c(p) = Asc(k.CharAt(p))         
Next

t = DateTime.now               'Start benchmark
'===============================================
Do While i < l
    i = 0
    s = s + 1
    RndSeed(s)
    Do While i < l And Rnd(48, 123) = c(i)
        i = i + 1
    Loop
Loop
'===============================================
t = DateTime.Now - t           'End benchmark

Log("Key  : " & k)
Log("Seed : " & s)
Log("Time : " & (t * 0.001) & " sec")

1.207 seconds @ Core i7 3770
 
Last edited:

wonder

Expert
Licensed User
Longtime User
Here's the C version, based of the code above.
B4X:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef enum { false, true} bool;

// Returns a random lower-case character
int rndChar()
{
    return 97 + (rand() % 26);
}

int main()
{
    char  *k =  "erel";
    size_t l =  strlen(k);
    size_t i =  0;
    int    s = -1;
    while (i < l && !(i = 0)) {
        srand(++s);
        while (i < l && rndChar() == k[i] && ++i);
    }
    return s;
}
Way more elegant than before! :)
 
Last edited:
Top