0

Issue:

I am trying to create RNG random number generator,using javascript, my objective is to have RNG which generate Random number using seed value , which will give almost impossible to predict the pattern of the random number.

The code starts here:


    var digits = 10,
        seed = 9999199999;
    
    function nextRand() {
        var n = (seed * seed).toString();
        while (n.length < digits * 2) {
            n = "0" + n;
        }
        var start = Math.floor(digits / 2),
            end = start + digits;
        seed = parseInt(n.substring(start, end));
        return seed;
    }
    
    function nextRandFloat() {
        return nextRand() / 9999999999;
    }
    for (var i = 0; i < 20; i++) {
        console.log(nextRandFloat());
    }
    var results = [];
    for (var i = 0; i < 100000; i++) {
        var rand = nextRand();
        if (results[rand]) {
            break;
        }
        results[rand] = true;
    }
    console.log(i);


I have run this code in many javascript editor but I didn't get any out put can anyone please explain what i did mistake.

2
  • Please format your code correctly and check your browser console. What error messages do you get? Commented Sep 10, 2017 at 6:52
  • Press CTRL + Shift + C if you are on Windows, then click on console, and you should see the output. Commented Sep 10, 2017 at 6:54

2 Answers 2

0

your code seems working to me

var digits = 10;
var seed = 9999199999;

function nextRand() {
    var n = (seed * seed).toString();
    while (n.length < digits * 2) {
        n = "0" + n;
    }
    var start = Math.floor(digits / 2),
        end = start + digits;
    seed = parseInt(n.substring(start, end));
    return seed;
}

function nextRandFloat() {
    return nextRand() / 9999999999;
}
for (var i = 0; i < 20; i++) {
    console.log(nextRandFloat());
}
var results = [];
for (var i = 0; i < 100000; i++) {
    var rand = nextRand();
    if (results[rand]) {
        break;
    }
    results[rand] = true;
}
console.log(i);

Sign up to request clarification or add additional context in comments.

Comments

0

If you run this code on a webpage without enclosing it in <script> tags then the browser would fail to execute the script. You can run this script here at Stackoverflow, jsfiddle.net and at codepen.io without those tags because of the way those environments were designed.

Furthermore, you won't see any output on the webpage because the script is having the output logged to the console. If you use Google Chrome, go to Developer Tools and you can see what output was logged. When I checked the console, the following output appeared:

 0.0006200016000620002
 0.03844019840384402
 0.7648853031764885
 0.952689837895269
 0.7927047390792705
 0.08032330580803233
 0.18334546401833454
 0.5559169375555917
 0.364139937836414
 0.789430098778943
 0.9880733491988073
 0.894320169089432
 0.8564680188856468
 0.7467227197746723
 0.4820116164482012
 0.5198344540519835
 0.7859565478785957
 0.7695029693769503
 0.4819761516481976
 0.10107111461010711
 20570

Note, you don't always have to hard-code the script tags, you can use JavaScript or jQuery to dynamically add them per this discussion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.