Bug Issue: Random number generation in Android

Pages: 1


  • Subscriber - Stencyl Studio
  • *
December 27, 2020, 12:23:49 pm
Please see post http://community.stencyl.com/index.php/topic,61477.new.html#new

I have a problem generating random numbers in Android. It happens either with integers or float. So I made up a quick demo project:

It has a starting scene with a button that takes you to the scene where the RNG is. In a When Created I generated 10 random numbers between 1 and 100. Every time the app was launched it generates the same 10 numbers: 1, 65, 94, 35, 48, 67, 37, 33, 1 and 30. If I reload the scene from within the scene, the numbers are different. If I go back to the starting scene and then back to the RNG, numbers are different. Only when re launching the app the numbers are the same.

Tested in Mac and Flash, no problem whatsoever. Didn't test in iOS since generating the certificates and the profiles just for this is a PITA.


New attachment added by mbergoei - December 27, 2020, 12:24:14 pm

New attachment added by mbergoei - December 27, 2020, 12:24:54 pm


  • Master Stencyler
  • *
January 22, 2021, 01:56:10 am


  • Subscriber - Stencyl Studio
  • *
January 22, 2021, 03:53:31 pm
Hi Justin,

For what I understand, it is not yet fixed?

How could I implement the code suggested in https://github.com/Kode/Kha/blob/master/Sources/kha/math/Random.hx in Stencyl?



  • Master Stencyler
  • *
January 22, 2021, 06:18:58 pm
I just stumbled upon this too, with a suggestion from 3 years ago. I guess this has been a problem for a long time!


To try out the first posted solution:

In Stencyl, Create New > Behavior > Freeform Mode
Name: CPPRandomSeed
Classname: CPPRandomSeed
Package: scripts

Create the script, and replace the entire file's content with this:

Then add "package scripts;" on a new line at the top of the file, to match Stencyl's default package name.

In your game's starting code, just place a code block with this content:
Code: [Select]

This should allow you to use Stencyl's built-in random blocks, which will be less trouble than trying to use the Kha class.


  • Subscriber - Stencyl Studio
  • *
January 23, 2021, 04:46:03 pm
Works like a charm! Way to advanced for me  :)

Thanks Justin!

Issue updated by Justin - April 04, 2021, 06:41:52 pm
  • Tags added: 4.1.0

Pages: 1


  • Reported
    December 27, 2020, 12:23:49 pm
  • Updated
    April 04, 2021, 06:41:52 pm

  • View Status
  • Type
  • Status
  • Priority
  • Version
  • Fixed in
  • Assigned to
  • Category



* RNG.stencyl (463.21 kB - downloaded 238 times.)
* log-2020-12-27.zip (1064.77 kB - downloaded 238 times.)