Python Forum

Full Version: Randomly changing numbers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, this is my first post here. I come from an art background and am an extremely interesting in intersecting both art and programming.
I am currently working on a project where I wish to alter numbers from a list by adding a +1 or -1 to that number to a few (lets say 500) randomly selected numbers out of thousands of numbers. I am familiar with the random number generator, however unsure what my next step to alter current numbers would be. Thanks.

These are a few of the numbers I wish to edit:

-0.588700 -0.394800 2.730300 -0.491500 -0.284200 2.716800 -0.629800 -0.231300 2.574700
-0.588700 -0.394800 2.730300 -0.629800 -0.231300 2.574700 -0.711400 -0.404200 2.647700
-0.491500 -0.284200 2.716800 -0.359600 -0.120500 2.692200 -0.513300 -0.040000 2.517700
-0.491500 -0.284200 2.716800 -0.513300 -0.040000 2.517700 -0.629800 -0.231300 2.574700
-0.359600 -0.120500 2.692200 -0.213300 0.069200 2.663800 -0.375400 0.168200 2.469000
-0.359600 -0.120500 2.692200 -0.375400 0.168200 2.469000 -0.513300 -0.040000 2.517700
-0.213300 0.069200 2.663800 -0.057400 0.270200 2.639300 -0.219000 0.379800 2.436900
-0.213300 0.069200 2.663800 -0.219000 0.379800 2.436900 -0.375400 0.168200 2.469000
Without seeing some code, I have no idea how to help, as adding/subtracting a number is a fairly simple process.

Are these numbers in a list, and you have an index you want to change? numbers[index] += 1
I do apologise for my vague question. I am a beginner when it comes to programming so mainly I follow tutorials, however I was not able to find something similar to what I want. I have around 300,000 numbers and I want to randomly change around 500 numbers at a time, either adding a +1 or -1 to that number without changing the structure of the numbers.
import random
list_of_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(len(list_of_numbers)):
    rand = random.choice([-1, 1])
    list_of_numbers[i] += rand
print(list_of_numbers)
Something like this? Undecided
At the moment I have that, which shows a list of numbers that I have, and separates them. What I am trying to do is take numbers from that list and alter some randomly.
fr = open('cell.raw','r')
cell = fr.read()
listofcell1=(cell.replace('\n', ' '))
listofcell2=listofcell1.split(' ')
print(listofcell2)
fr.close()