What is a good way to predict pseudo random numbers with neural networks?

Question:

Today I tried to see if a neural network could predict the next random number generated from a sequence of 20 previous random numbers. However, when I actually predicted using the model it simply returned the most common number. I’m very new to keras and tensorflow so I have no idea how I could approach this problem, or if it is even possible. Here is the code.

from random import randint
import numpy as np
import tensorflow as tf

def generate_sequence(length=1000000):
    rList = []
    for i in range(length):
        rInt = randint(0, 99)
        if rInt >= 0 and rInt <= 47:
            rList.append(0)
        elif rInt > 47 and rInt <= 71:
            rList.append(1)
        elif rInt > 71 and rInt <= 87:
            rList.append(2)
        elif rInt > 87 and rInt <= 95:
            rList.append(3)
        else:
            rList.append(4)
    return rList

data = generate_sequence()
train_data = data[:800000]
test_data = data[800000:]
x_train = []
y_train = []
x_test = []
y_test = []

for i in range(0, 799980, 20):
    x_train.append(train_data[i:i+20])
    y_train.append(train_data[i+20])

for i in range(0, 199980, 20):
    x_test.append(test_data[i:i+20])
    y_test.append(test_data[i+20])

x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)

x_train = np.asarray(x_train)
y_train = np.asarray(y_train)
x_test = np.asarray(x_test)
y_test = np.asarray(y_test)

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(625, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(125, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(25, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(5, activation=tf.nn.softmax))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)

val_loss, val_acc = model.evaluate(x_test, y_test)
print(val_loss, val_acc)

prediction = model.predict([x_test])
for pred in prediction:
    print(np.argmax(pred))
Asked By: SOME_LIMES

||

Answers:

I don’t think you can predict numbers from Python’s random module based on past outputs. The random module uses a Mersenne Twister that repeats only after 2^19937-1 numbers. A neural network won’t be able to predict the following numbers based on previous numbers.

The reason your neural network is returning the most common number is that since there is no correlation between any given 20 previous numbers and the following, the best the neural network can do is guess the most common since that is what will score highest in training.

If your goal is to crack Python’s random module, perhaps this answer that suggests calculating the state of the Mersenne Twister may be helpful.

Answered By: fishcakes