How do I generate a random character string

0 favourites
  • 12 posts
From the Asset Store
2d mushroom sprite 2d game mushroom character enmy sprite game art
  • Hi everyone,

    I'm currently designing a game for which I need to put an online database with highscores. Thanks to ArcadEd 's excellent tutorial, everything went smoothly, but now I face another problem.

    The game exports scores to the database at the end of every level. This means that if you scored 600 points in level 1 and 700 in level 2, your total score will be 1300. However, as the score is send at the end of every level, this single session will export scores of 600 and 1300 for this player. This is problematic because of course I want only one score to be send per game session, not one per level!

    Is there a way to avoid this?

    One way I thought about doing this is to create a random (long) string of characters and add this to the database under "sessionID" so that each session correspond to a single ID and the score is sent to the same place of the database, thus avoiding multiple scores to be send from the same game.

    Is there a better way to do this that you can think of?

    If not... well I have troubles creating this random string of characters (it should use most ascii characters). I should probably make a loop and append a random character at the end of a variable, but is there any way easier to select any random character than having a random number between 33 and 126 and extracting the character corresponding to this ascii code?

    Not exactly sure how to translate this into Construct 2 either... Or maybe is there any string generator out there in the plug ins?

    Thanks for any help!

  • What you want is called a "hash", it is a random string generated using a seed input. You can use the CB Hash plugin to do this easily in C2.

    You can just use random(1000000) as a seed if you need a randomized hash.

  • gumshoe2029

    Thanks for the reply. The CB Hash plugin is pretty close from what I am looking for. But if I understand this plugin correctly (which I may not) it doesn't seem to me that it generates random strings. As you state yourself, it uses a seed and generate a character string based on it, right?

    So if I use random (1000000) as a seed, well... It does the same job as just using random(1000000) as the unique code in my case. I don't necesarilly need to have that code scripted.

    But anyway, thanks for pointing out the CBHash plugin to me. I installed it and it will be useful someday for sure. <img src="{SMILIES_PATH}/icon_e_wink.gif" alt=";)" title="Wink">

    EDIT :

    If anyone else need to do this, here is what I did :

    On start of layout

    repeat 30 times --> text > append keyboard.stringfromkeycode(floor(random(42)+48))

    set sessionID to text.text

    And now I've got my text variable sessionID with a unique 30 character string composed of any random character chosen from numbers from 0 to 9, letters from A to Z and a bunch of special characters.

    If you need more information about keyboard.stringfromkeycode and stuff, you should look at this excellent tutorial by Waltan :

    https://www.scirra.com/tutorials/739/your-own-textbox

  • You are exactly correct in that it doesn't generate random strings, which is why I said you have to seed it with the random number. It just takes the text value of that number and turns it into a more random string.

    Uniform random number generation is not cryptographically secure it you are wondering, so do not use it for things like hashing passwords (and even session ids is probably not a good idea). But this is the 'easy mode' answer.

    Even your example is not cryptographically secure and could probably be guessed by a good cryptanalysis program.

    If you want to put a little work into it, I can give you more options.

    If you want to search yourself, the key phrase is "cryptographically secure" for any library that you are looking at.

  • gumshoe2029

    Thanks a lot for your concern, but I don't really need something 100% secure. I use this session ID just to figure out that the score of this game has already been injected in the database sooner. It isn't related to password or anything that need high security.

    But it is very possible that one day I'll need something cryptographically secure, so I'll refer to this post if it happens.

    Thanks again!

  • Sessions can be hijacked if the session ID is not cryptographically secure. Just FYI.

    https://en.wikipedia.org/wiki/Session_hijacking

  • Thanks for the link. I'll check with the technical guys if this is required.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • It is only required if you want your application to be secure, and not have clever folks hacking your users.

  • This code snippet provide a quick way to generate UUID.

    http://jcward.com/UUID.js

    You could use browser ExecJS to use it or wrap the snippet in a simple plugin with a single expression.

  • I had the need to generated v4 UUIDs for a project I was working on so I created a c2addon. Link below.

    This addon contains a global plugin which allows you to insert version 1 or version 4 UUIDs via an expression.

    http://zerglabs.github.io/c2-addons/uuid.c2addon

  • This way I generate a random letter from a constant string.

    mid("ABCDEFGHIJKLMNOPQRSTUVWXYZ", random(0,26), 1)

  • Thanks for the reply. I've been using

    Keyboard.StringFromKeyCode(floor(random(42)+48))

    for the last 2 years or so to generate random characters, but your option is probably simpler.

    I suppose the index should be random(0,25) (instead of (0, 26) ) in your example, though?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)