-1

I need to write a script that generates random numbers between 1-257000 and stops when a certain number occurs telling me how many numbers it generated so far.

i manged to get this far but can't seem to get it to stop or count

x=1
while x < 257000:
    import itertools
    import random

    def random_gen(low, high):
        while True:
            yield random.randrange(1, 257000)

    gen = random_gen(1, 100)
    items = list(itertools.islice(gen, 10))

    print items
    x = x+1

Thank you so much for your help

  • 1
    Why are you creating a function in the while loop and repeatedly importing? random_gen takes two arguments that you never use and you have no break condition so what do you expect to happen, what is the *certain number*? – Padraic Cunningham Oct 18 '16 at 10:55
  • 1
    I see you just copy/pasted from http://stackoverflow.com/a/22842533/2141635, you should try to understand code instead of blindly copy/pasting and then getting others to figure the code out for you. – Padraic Cunningham Oct 18 '16 at 11:01
  • your while loop break is up to x; if x equals 25700 your loop breaks. so it has not been considered that if certain number generates the loop is over. so you have wait as long as x=25700 and your code stop – Mehrdad Dadvand Oct 18 '16 at 11:01

2 Answers2

2

Huh. A few flaws (or at least unclear spots) in your code.

  1. You run your loop max 257000 times. Even though the probability is low, there is a chance that you don't hit the number you seek in the loop.

  2. Move your import statements out of your loop, no need to have python check loaded modules each round.

  3. You use a generator for choices of a list (randrange) where you can simply use a randint() call.

  4. You define a closed function within your loop which creates a new function at a new memory address each round.

  5. You slice your results into lists of 10 elements each; is this for printing, or do you actually need your random integers grouped into such lists?

A very simple and straightforward implementation of your described problem could be:

import random
num = 0 # Our counter
certain_number = 123456 # The number we seek

while True: # Run until we break
    # Increment for each new step
    num += 1

    # Generate a single number from the given range
    random_number = random.randint(1, 257000)

    if random_number == certain_number:
        # Break if we hit it
        break

print('Hit after {} tries.'.format(num))

>>> Hit after 382001 tries.
jbndlr
  • 4,965
  • 2
  • 21
  • 31
0

First, put your import statements and your function definitons outside your while-loop. That's being super redundant.

>>> def random_gen(low,high):
...   while True:
...     yield random.randrange(low,high)
... 
>>> lucky = 7
>>> rg = random_gen()   
>>> rg = random_gen(1,1000)                 
>>> next(itertools.dropwhile(lambda t: t[1] != lucky, enumerate(rg, 1)))
(811, 7)
>>> 

Here's another run, just for fun:

>>> rg = random_gen(1,257000)
>>> n,L = next(itertools.dropwhile(lambda t: t[1] != lucky, enumerate(rg, 1)))
>>> n
22602
>>> L
7
>>>
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • You will need to explain your code as the OP did not really write the code in the question so they will have no idea again what is happening. – Padraic Cunningham Oct 18 '16 at 11:07
  • @PadraicCunningham Surely, if the OP is using generators then they will understand this code. They wouldn't just blindly copy and paste code and insert it into a while-loop randomly! – juanpa.arrivillaga Oct 18 '16 at 11:10
  • 2
    Removing the arguments from `random_gen` will make `rg = random_gen(1,1000)` fail – Holloway Oct 18 '16 at 11:11
  • @juanpa.arrivillaga, that is exactly what they have done, http://stackoverflow.com/a/22842533/2141635 look familiar? There code is not even close to correct, it is a copy/paste jigsaw puzzle that is missing a lot of pieces. They also don't even know where to define functions nor their imports so I am 100 percent confident they have no idea what is happening in your code or their own for that matter. – Padraic Cunningham Oct 18 '16 at 11:11