Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Repeatable Random Number

    Coding
    7
    23
    1103
    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.
    • F
      faz808 F last edited by

      I'm trying to plot 255 random points representing stars in a galaxy map similar to Elite 1.
      I have set up an array galaxy_x[256] = random(gWidth) and galaxy_y[256] = random(gHeight).
      On each run of the code a different set of points are plotted. Is there any way of repeating the same random numbers.
      I think I'm hoping for a seed or something similar. Thanks

      1 Reply Last reply Reply Quote 2
      • Tri0pticon
        Tri0pticon last edited by

        Couldn’t you save them to a file and load them when needed?

        1 Reply Last reply Reply Quote 1
        • pianofire
          pianofire Fuze Team last edited by

          Search for random_set_seed on the forum for a post where I suggest a way of doing this

          1 Reply Last reply Reply Quote 0
          • F
            faz808 F last edited by

            Thanks for the suggestion, but this is from the book "The C Programming Language," by Kernighan and Ritchie:
            int rand() { random_seed = random_seed * 1103515245 +12345;
            return (unsigned int)(random_seed / 65536) % 32768; }
            might be adaptable...

            1 Reply Last reply Reply Quote 0
            • Martin
              Martin Fuze Team last edited by

              Well, you asked for advice, so I would check out what was suggested, but if you prefer to go with what you want, then sure, just type it in using Fuze syntax:

              // declare random_seed somewhere as a global
              function myRand()
                  random_seed = random_seed * 1103515245 + 12345
              return (random_seed / 65536) % 32768
              
              1 Reply Last reply Reply Quote 0
              • F
                faz808 F last edited by

                I'm sorry if I seem to have given the impression that I was ungrateful for the suggestion by pianofire. That was certainly not my intention. It's just that all code possibilities are worth exploring and some are better than others.
                I have tried the suggested code - pianofire - and it runs flawlessly, Thanks...

                1 Reply Last reply Reply Quote 2
                • pianofire
                  pianofire Fuze Team last edited by

                  I have suggested that a way of setting the random seed be added to the language

                  1 Reply Last reply Reply Quote 1
                  • F
                    faz808 F last edited by

                    Great! Thanks again.

                    1 Reply Last reply Reply Quote 1
                    • F
                      faz808 F last edited by

                      Any news on the next update? Thanks.

                      1 Reply Last reply Reply Quote 0
                      • Retrocade_media
                        Retrocade_media F last edited by Retrocade_media

                        You could loop through the length of a string? So like
                        for i = 0 to len(seed) loop
                        Seed[:i:] * (i*14)
                        Repeat

                        That way each number would return a set value. If you add more parameters it could output different numbers based on the scenario, but it'd be the same number per seed
                        (Also this is using the advanced text functions, more about them in the hints and tips section on this forum)

                        1 Reply Last reply Reply Quote 0
                        • PB____
                          PB____ last edited by PB____

                          I suspect you could use stringhash(value) to generate pseudo random numbers:

                          string seed = str(random(1000000000))
                          function rng(low, high)
                              int value = stringhash(seed)
                              seed = str(value)
                              // following line limits "value" to be between high and low:
                              value = (abs(value) % abs(high-low)) + low
                          return value
                          

                          EDIT: just checked, and similar numbers give similar patterns, so this might not be what you want.
                          For example, the results of StringHash("0") to StringHash("9") are 177621 to 177630 (counting). However if your initial seed is random, the sequence of the example code above at least looks somewhat random to me...

                          1 Reply Last reply Reply Quote 0
                          • F
                            faz808 F last edited by

                            Deleted my Elite clone as it taking up too much time. Also the code was a complete mess. Now unfortunately, when I try to access the Map Editor or Image Editor Fuze closes "because an error occurred". I've done a Switch reboot but the problem remains. The crash also occurred when I made two copies of my original (approx. 800 lines) code. If I tried to reload and run the original file a crash would occur but I learnt to live with that. Could my two copies still be present somewhere? I've just loaded and run KatRacer ok. Back to tools and image editor - crash again. Any ideas, Sir. Thanks.

                            M 1 Reply Last reply Reply Quote 0
                            • M
                              Maxwello F @faz808 last edited by

                              @faz808
                              Try creating a new project and putting in some basic code like load image, create sprite, variable =1, clear, update, repeat..
                              Then try.
                              Whilst we wait for a better assist

                              1 Reply Last reply Reply Quote 0
                              • F
                                faz808 F last edited by

                                Update. I've deleted all the downloaded examples (not the included examples!) and the problem has disappeared. One of the downloaded files was causing the crash. I had about a dozen so which one was causing the problem is anyone's guess.

                                pianofire 1 Reply Last reply Reply Quote 1
                                • F
                                  faz808 F last edited by

                                  I've just made two copies of a small Print "hello" program. No crashes. The problem was definitely caused by one of the downloaded code examples.

                                  1 Reply Last reply Reply Quote 0
                                  • pianofire
                                    pianofire Fuze Team @faz808 last edited by

                                    @faz808 Was this something you downloaded by entering a code or from a friend share?

                                    1 Reply Last reply Reply Quote 0
                                    • F
                                      faz808 F last edited by

                                      Entering a code.

                                      pianofire 1 Reply Last reply Reply Quote 0
                                      • pianofire
                                        pianofire Fuze Team @faz808 last edited by

                                        @faz808 OK I will see if I can identify the culprit

                                        1 Reply Last reply Reply Quote 0
                                        • F
                                          faz808 F last edited by

                                          I should have removed them one by one and retested. Sorry about that.

                                          1 Reply Last reply Reply Quote 0
                                          • pianofire
                                            pianofire Fuze Team last edited by

                                            @faz808 No problem

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post