• Login
Community
  • Login

The replacement of the number X with a random number in an interval

Scheduled Pinned Locked Moved General Discussion
12 Posts 4 Posters 3.1k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D
    David Costache
    last edited by David Costache Apr 6, 2022, 6:47 AM Apr 6, 2022, 6:44 AM

    I want to know if there exists an expression to replace, for example, the numbers Y which repeat multiple times with random ones in a certain interval. For example, 10-20.
    I do not want, when it finds the Y numbers repeated multiple times, to be still replaced with a number which is the same, but for all to be different.

    To be more clear:

    Find the number 740
    
    Found (500+ lines):
    740
    740
    740
    etc.
    
    Replace the numbers 740 (inside the 10-20 interval):
    11
    14
    19
    
    T 1 Reply Last reply Apr 6, 2022, 7:03 AM Reply Quote 0
    • T
      Terry R @David Costache
      last edited by Terry R Apr 6, 2022, 7:04 AM Apr 6, 2022, 7:03 AM

      @david-costache
      This would be a job for a programming language, most of which include random number generators. If looking at the Notepad++ environment PythonScript is one that comes to mind first, although others also exist.

      There is a plugin that performs random string generation but as far as I can see it would not help here.

      Have a search in this forum using words like (python pythonscript, code, program), you will see many pythonscript programs supplied by various forum members which help out in various capacities. Maybe one of those can form the basis of what you need. Someone here might supply something, but I wouldn’t plan on it being so as only a small group of forum members dip their finger in programming. If it could be done with a regular expression many would be happy to lend a hand.

      Terry

      D 2 Replies Last reply Apr 6, 2022, 8:28 AM Reply Quote 1
      • D
        David Costache @Terry R
        last edited by Apr 6, 2022, 8:28 AM

        This post is deleted!
        E 1 Reply Last reply Apr 6, 2022, 8:37 AM Reply Quote 0
        • D
          David Costache @Terry R
          last edited by Apr 6, 2022, 8:34 AM

          @terry-r Thank you for your response. I will try with the PythonScript plugin.

          1 Reply Last reply Reply Quote 0
          • E
            Ekopalypse @David Costache
            last edited by Apr 6, 2022, 8:37 AM

            @david-costache

            Random and unique doesn’t fit.
            In your case it is a MUST that each replaced value is a UNIQUE number?

            D 1 Reply Last reply Apr 6, 2022, 8:40 AM Reply Quote 0
            • D
              David Costache @Ekopalypse
              last edited by Apr 6, 2022, 8:40 AM

              @ekopalypse It is not necessary for each replaced to be unique, it may repeat once, twice.

              E 1 Reply Last reply Apr 6, 2022, 8:46 AM Reply Quote 0
              • E
                Ekopalypse @David Costache
                last edited by Apr 6, 2022, 8:46 AM

                @david-costache

                from Npp import editor
                from random import randint
                editor.rereplace(r'\b740\b', lambda x: randint(10,20))
                

                This would replace a value 740 with a random number from 10 to 20.

                D 1 Reply Last reply Apr 6, 2022, 9:15 AM Reply Quote 3
                • D
                  David Costache @Ekopalypse
                  last edited by Apr 6, 2022, 9:15 AM

                  @ekopalypse Thank you a lot for the help! I have never until now used PythonScript, or any other plugin for Notepad++. If you could also tell me succinctly which are the steps on how I could implement the code such that it could do what I want, that would be wonderful.

                  E 1 Reply Last reply Apr 6, 2022, 9:23 AM Reply Quote 0
                  • E
                    Ekopalypse @David Costache
                    last edited by Ekopalypse Apr 6, 2022, 9:24 AM Apr 6, 2022, 9:23 AM

                    @david-costache

                    the steps are

                    • Plugins->PythonScript->New script (save as dialog opens).
                    • Give the script a meaningful name and save it
                    • Copy the code as indicated into the newly created document and save it
                    • Open a file with the content you want to change and run
                    • Plugins->PythonScript->Scripts->YOUR_SCRIPT_NAME

                    If an error occurs, open the console (Plugins->PythonScript->Show Console) and see what it is complaining about.

                    D 1 Reply Last reply Apr 6, 2022, 5:51 PM Reply Quote 4
                    • D
                      David Costache @Ekopalypse
                      last edited by David Costache Apr 6, 2022, 5:51 PM Apr 6, 2022, 5:51 PM

                      @ekopalypse I am getting this error

                      ModuleNotFoundError: No module named 'Npp'
                      
                      P 1 Reply Last reply Apr 6, 2022, 5:58 PM Reply Quote 0
                      • P
                        PeterJones @David Costache
                        last edited by PeterJones Apr 6, 2022, 5:59 PM Apr 6, 2022, 5:58 PM

                        @david-costache ,

                        Did you try to run your script with your own python.exe? Or did you follow @Ekopalypse’s instructions, where you run it inside Notepad++'s PythonScript plugin, using the PythonScript menus? Because if you ran it with PythonScript and it couldn’t find the Npp module, something is seriously wrong with your PythonScript installation.

                        If you were really running from within PythonScript, please use Plugins > Python Script > Show Console, and give us a screenshot of that, including both the Python initialization and the error in the same screenshot. That might help us to give you ideas on how to fix your environment.

                        Other useful information to help with debug would be a screenshot of Plugins > Python Script > About, and maybe even your ?-menu’s Debug Info copied to clipboard and pasted here.

                        D 1 Reply Last reply Apr 6, 2022, 6:16 PM Reply Quote 4
                        • D
                          David Costache @PeterJones
                          last edited by Apr 6, 2022, 6:16 PM

                          @peterjones Oh, you are perfectly right. I have begun to run the script with my own python.exe
                          It functioned perfectly what @Ekopalypse has told me. Likewise, I have had one more problem, but I have realised what I was doing wrong. When I was saving the script, I was saving it anywhere else other than in *\Notepad++\plugins\PythonScript\scripts and it was not showing up in my PythonScript script menu. I thank the two of you very much.

                          1 Reply Last reply Reply Quote 5
                          4 out of 12
                          • First post
                            4/12
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors