0

Is there any possible way to choose a random number in Python without using a module? So far I have:

import numpy as np
numbers = np.random.choice(range(5), 10000000, p=[0.1, 0.05, 0.1, 0.7, 0.05])

but that uses a module. Any advice?

Emil
  • 41
  • 2
  • 10
  • 1
    Unless you want to write your own random number generator (which requires math than I can do in 5 minutes), you'll have to import something (`numpy`, `random`, `pandas`, etc). Of course, `random` comes with the python standard library, whereas the others have to be installed after the fact – inspectorG4dget Jan 14 '17 at 00:38
  • 1
    Do you mean using a builtin module? Or just writing an algorithm? I would never advise writing your own random number generator unless you really, really know what you are doing. Testing them is horrible. – Namey Jan 14 '17 at 00:39
  • I used numpy - I want a generator from scratch. (I'm new to python, and have only coded in it for a few months) – Emil Jan 14 '17 at 00:39
  • @Namey - An algorithm might be best – Emil Jan 14 '17 at 00:40
  • 2
    Python uses a [Mersenne Twister](https://en.wikipedia.org/wiki/Mersenne_Twister) to generate random numbers, so you could use that algorithm – inspectorG4dget Jan 14 '17 at 00:41
  • 2
    @Emil I assume this must be for a course project. I can think of no reason why you would otherwise ask this question and avoid using the off-the-shelf solution. If this is related to a class exercise, I would recommend doing your own legwork- you will learn more that way. – Namey Jan 14 '17 at 00:43
  • @Namey I'm actually just doing this for the reason of trying to do it from scratch to learn, but as a new python-coder I do not know the possible syntax and hints are in order. -Emil – Emil Jan 14 '17 at 00:45
  • You can code the classic shift register algorithm: http://www.cs.miami.edu/home/burt/learning/Csc609.022/random_numbers.html – DYZ Jan 14 '17 at 00:47
  • 3
    Implementing a random-number probably isn't the best project for learning Python. – juanpa.arrivillaga Jan 14 '17 at 00:47
  • Python's source code is written in c code, so you can't really re-use the source code. You could use something that's time based – PMARINA Jan 14 '17 at 00:47
  • @DYZ Thanks, I'll look at that. – Emil Jan 14 '17 at 00:49

2 Answers2

1

I would highly recommend using Python's built-in random number module, which comes stock with every install. I cannot think of a case where this would be a bad idea, unless you are dealing with some stripped down special build of Python. It will be faster than anything you can implement yourself in Python probably, and has been tested and used regularly. Testing your own random number generator is madness when you can use one that exists in a couple of lines.

Namey
  • 1,172
  • 10
  • 28
  • OP asked how to implement an algorithm for a pseudo-random number generation. Your oppinion is irrelevant in this case – TopchetoEU Jan 09 '22 at 13:10
  • @TopchetoEU He asked how to do it without a module (and gave a NumPy example that requires installing an external module). I suggested using the built-in module available for basically all flavors of Python. I also suggested never, ever building your own random generator when a solid one is already built, which is advice definitely worth drilling in. Even for practice, why practice things you would never do and which prevent applying normative best practices for code (e.g., like automated tests, which aren't ideal tools to test an RNG). – Namey Jan 11 '22 at 06:05
0
# RANDOM NUMBER GENERATOR

the_set = set()

for i in range(10):
    the_set.add(str(i))

for e in the_set:
    print(int(e))
    break

This does not use a module! So this works by creating a set(in which case the values are not ordered), then just prints the first value that it can find and the break is here to stop it from printing all the values of the set.

You reason why var i is added as a str is because if we were to add it as an int then the numbers would get ordered and the first or the smallest number would get printed.

Note that the number 10 will not generate, because it is until 10 and not including it! In order to print also 10, you would need too make the range range(11).

And 0 will also have a chance to get printed, if you don't want to 0 to appear just make the range range(1, 10).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    The values aren't guaranteed to be ordered in theory but they aren't guaranteed to follow any sort of uniformly random permutation, either. In practice, I find that I keep getting `0` as the output with your code. Good try, though! I like the premise and I'm sure it would work with some implementations of `set` (in some other version of Python, or maybe some other language). – Salmonstrikes Sep 04 '21 at 18:03
  • Here is some deep discussion on that: https://stackoverflow.com/questions/2860339/can-pythons-set-absence-of-ordering-be-considered-random-order – Flair Sep 04 '21 at 22:19
  • I ran this exact same code on multiple online ides, ran it on pycharm, and in the terminal and it seems to work! – Anon Angel Sep 11 '21 at 18:13