-1

I want to make a random integer in python, not using any module

remtii
  • 7
  • 1

2 Answers2

0

If you don't want to use a library function, then it is probably simplest to write your own Linear Congruential RNG. Use one of the suggested set of parameters -- don't pick them yourself because a bad parameter set will result in not very random numbers.

To seed the generator you can either use user input or else take something from the system clock using the finest grain you can: microseconds in preference to milliseconds.

rossum
  • 15,344
  • 1
  • 24
  • 38
-2

I'm pretty sure that that's not possible. If you don't want to use the random module you can try using requests. In theory, you can get a random integer by creating and hosting a website (use 000Webhost for free hosting) and upload a javascript script that adds 1 every second. You can do something like this:

function sleep(milliseconds) {
  const date = Date.now();
  let currentDate = null;
  do {
    currentDate = Date.now();
  } while (currentDate - date < milliseconds);
}


var i=0;
var x = 1;
while (i < 10) {
    var x+=1;
    document.write(x);
    sleep(1000);
}

Then to get the value in python you can do this:

import requests

r = requests.get(url)
random_integer = r.text
Quessts
  • 440
  • 2
  • 19
  • 2
    super convoluted and not even slightly random, you'd have to go through a Pseudo Random number generator with your millisecond based number as a seed to get some randomness. Furthermore, if your website only does that, you can just use the UTC datetime directly from python without having to host a website. – Manuel Faysse Apr 02 '21 at 08:22