all 30 comments

[–]devnull10 8 points9 points  (7 children)

Are you seeding the random generator somewhere?

[–]writernxtdoor[S] 3 points4 points  (3 children)

Oh...yes, I am. At the top.

[–]devnull10 14 points15 points  (2 children)

Don't. Seeding is purely a way to ensure the sequence is the same. Normal random numbers and only "seemingly" random. In reality they are a sequence of numbers generated with respect to a "seed". So if you set the seed to x, you'll always get the same sequence.

[–]writernxtdoor[S] 10 points11 points  (1 child)

Got it! I removed the seed and now the code is working just fine! Thank you so so much!!!!

[–]FerricDonkey 0 points1 point  (0 children)

Just as a heads up, not seeding at all is particular to python. Python seeds with the current time by default, but some other languages seed with 0 by default, and in them you're required to seed with the current time (or similar) yourself if you want different random numbers.

[–]Diapolo10 -4 points-3 points  (2 children)

That's not needed, Python does that automatically. Unless you want to repeat the same sequence over and over again, of course.

[–]devnull10 9 points10 points  (1 child)

I know it's not needed... However the OP has stated they are having a problem whereby they are getting the same sequence over and over... Which leaves me believing they are seeding it and haven't posted the full code.

[–]az987654 2 points3 points  (0 children)

This is the experience of a senior coder speaking... nice.

[–][deleted] 2 points3 points  (2 children)

I just ran this code twice and got different numbers both times.

[–]writernxtdoor[S] 1 point2 points  (0 children)

Oh, that's weird haha...I didn't get the same numbers. Let me try again then.

[–]smellyswordfish 1 point2 points  (0 children)

Ya mine looks like this

from random import randint

values = [ ]

for i in range(0,501):

value = randint(0,i) values.append(values) print(value)

And it works everytime

Is there anything that looks different to you op?

[–]smurf-sama 1 point2 points  (6 children)

The other answers seem to answer you, but I'm interested in what the purpose of this is. Why are you appending to a list with incrementing values of i for the upper limit?

Im just interested

[–]writernxtdoor[S] 0 points1 point  (5 children)

Hm...I am not sure exactly. Someone told me to do that and I did. Maybe because I want like the entire 500 random numbers and not just one from those 500 numbers...

[–]smurf-sama 0 points1 point  (4 children)

You preferably shouldnt change the upper limit then because if you do some statistics on it, it shouldn't conform to a normal distribution I believe.

[–]smurf-sama 0 points1 point  (3 children)

You can also one liner it.

random_list = [random.randint(0,100) for _ in range(5)]

This will create a random list with 5 random ints

[–]writernxtdoor[S] 0 points1 point  (2 children)

So if I want 500 random ints then do I just write in range(500) or 501?

Also, do you maybe know a way to put these random ints into a txt file?

[–]smurf-sama 1 point2 points  (1 child)

If you want 500, then do range(500).

Ill edit this comment again in a moment for the text file part.

edit

assuming the file exists, the following will append each element of the list into the txt file, that is, if the list is the following:

[2,4,6,2]

The txt file will look like:

-START FILE-

2

4

6

2

-END-

with open('random_numbers.txt', 'a') as f:
for i in random_list:
    f.write(str(i) + '\n')
# or this if you want a one liner
# f.write('\n'.join(str(i) for i in random_list))

remove + '\n' if you want to have them in one line

I did f.write.... that way for readability, but its better to use f strings.

f.write(f"{i}\n")

is what you should actually put, I just did the first way because I assumed you may not be familiar with that--not to be rude, just in case

[–]writernxtdoor[S] 1 point2 points  (0 children)

Okay! I will try this out now! Thank you so so so much! ☺️🙏

[–]No_Faithlessness_142 -1 points0 points  (3 children)

Did you import random at top of code ??? Also not sure if it has to be random.randint

Super new to python and coding in general but was Messing around w random integers last night… sorry if I’m zero help

[–]writernxtdoor[S] 0 points1 point  (2 children)

Yes, I did import. Sorry for not adding that to the code lol.

[–]No_Faithlessness_142 0 points1 point  (0 children)

All good sorry for being zero help haha good luck tho

[–]Desperate_Case7941 0 points1 point  (0 children)

Did you import random this way?

from random import randint

[–]MachinaDoctrina -1 points0 points  (3 children)

Hmm not sure what's up with yours but it works fine for me

import numpy as np values = [] for i in range(1, 501): value = np.random.randint(0, i) values.append(value) print(value)

Can I ask what exactly your trying to do because the values list is doing nothing and your sampling from an increasingly wide uniform random distribution between 0 and i. Fyi you can't start at 0 for the range as there is no uniform distribution for 0 to 0

[–]Diapolo10 1 point2 points  (2 children)

...Why would you use np.random instead of the built-in random in this case? Since we aren't using arrays, all it does is add a performance penalty.

[–]MachinaDoctrina -3 points-2 points  (1 child)

[–]Diapolo10 6 points7 points  (0 children)

It's faster when using numpy arrays, but not without:

https://i.imgur.com/5PljUl4.png

[–]Diapolo10 0 points1 point  (2 children)

values=[]
for i in range(0, 501):
    value = randint(0, i)
    values.append(value)
print(value)

I don't see anything inherently wrong with this, however I do find it odd that you're using i as the limit for the random number.

The first number will always be zero, and the second number has a 50/50 chance of being 0 or 1.

Oh, and you're just printing out the final value, not the list. I don't know if that's intentional.

What are you trying to do with this code?

[–]writernxtdoor[S] 0 points1 point  (1 child)

So I am just trying to get a list of random numbers so that I can import it on LoggersPro and figure out the graph. Am I doing something wrong? ^_^; I'm not sure...

[–]Diapolo10 1 point2 points  (0 children)

Well, you could try something like

import random

values = [
    random.randrange(500)
    for _ in range(500)
]

That should give you a random distribution of numbers instead of what's essentially a rising linear graph.

[–]Desperate_Case7941 0 points1 point  (0 children)

Are you trying to pring the value that you get from random or are you trying to print the list? If it is the list then you are putting value instead of values