Bug Issue: Random number generation in Android


Pages: 1

mbergoei

  • 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.

Thanks



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


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


Justin

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


mbergoei

  • 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?

Thanks!



Justin

  • 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!

https://community.openfl.org/t/math-random-is-not-really-random-on-android/8684

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:
https://gist.github.com/player-03/929b900bd78d3ff641578b6e0c670c46

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]
CPPRandomSeed.seed();

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


mbergoei

  • 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

Details

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

  • View Status
    Public
  • Type
    Bug
  • Status
    New
  • Priority
    Normal
  • Version
    (none)
  • Fixed in
    (none)
  • Assigned to
    (none)
  • Category
    Engine

Tags


Attachments

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