Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Randomly changing numbers
#1
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
Reply
#2
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
Reply
#3
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.
Reply
#4
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
Reply
#5
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyaudio seems to randomly halt input. elpidiovaldez5 2 362 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Problem with code / audio is playing randomly, not matching csv requirements Daniel_kcr 2 623 Sep-07-2023, 05:09 PM
Last Post: deanhystad
  how to take a screnshot by Pyautogui automatically and randomly rachidel07 0 3,537 Feb-03-2021, 01:16 PM
Last Post: rachidel07
  module logging posting randomly DJ_Qu 2 2,208 May-14-2019, 06:41 AM
Last Post: DJ_Qu
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,718 May-09-2019, 12:19 PM
Last Post: Pleiades
  Can I search from Python, automatically and randomly generated keywords in Google? xX_Prophit_Xx 0 2,295 Sep-07-2018, 04:43 PM
Last Post: xX_Prophit_Xx
  Python seems to be randomly blowing up parmort 6 3,240 Aug-22-2018, 09:47 PM
Last Post: parmort
  How to print the names of assignments if they are randomly selected Kongurinn 2 3,311 Oct-22-2017, 07:31 AM
Last Post: buran
  Logic error when comparing randomly generated integers SadoDeomeoon 5 4,661 Jun-05-2017, 02:38 PM
Last Post: SadoDeomeoon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020