Why aren't my numbers random?

Monkey Forums/Monkey Programming/Why aren't my numbers random?

benmc(Posted 2011) [#1]
In my OnCreate I'm setting Seed=Millisecs() and then I'm using Rnd(range) to get about 5 random numbers at the beginning of the game.

The only problem is that every time I run the app, the same 5 random numbers come up.

Is it something to do with Millisecs?


therevills(Posted 2011) [#2]
Yep its due to Millisecs, Monkey's Millisecs start from the start of the application - not the system time.

That's why I added RealMillisecs to Diddy ages ago ;)

Seed = RealMillisecs()



SlopeOak(Posted 2012) [#3]
Is there still no better solution for this than using platform specific code?


Fryman(Posted 2012) [#4]
time how long it takes for the user to take an action then set seed based on that?


SlopeOak(Posted 2012) [#5]
That'd work for cases where you don't need the rand on startup, yes. Thanks for the idea!

Just seems odd that this is missing, as it's something a large percentage of games/apps do.


Gerry Quinn(Posted 2012) [#6]
How could you do it without platform-specific code? You are looking for information (a random seed) that cannot be provided by a deterministic programming language!

Of course RealMillisecs() could be put in mojo, but that is platform specific code just as much as Diddy.

The only way to do it without platform specific code is to ask the user to enter a value. And all user input involves platform specific code anyway!


Samah(Posted 2012) [#7]
Of course RealMillisecs() could be put in mojo, but that is platform specific code just as much as Diddy.

All translated Monkey code is platform specific, and Mojo is full of externs. As long as all your target platforms support the functionality and they work in pretty much the same way, it's fine.


SlopeOak(Posted 2012) [#8]
I guess I meant "how can you do it with just monkey/mojo" -- I'm aware that there's platform specific code under the hood :)

Sounds like it can't be done at the moment. Thanks!


Tibit(Posted 2012) [#9]
Since diddy is free I have no idea why it is not already included in Monkey, but I assume it has more to do with that Mark wants to only include things that he can support, and to do that he need to research this on all platforms, check for unintended side effects, review the code and also add this for every new target ever implemented.

I do recognise the logic behind not wanting to install an external library (Diddy or other) just because one happens to have need for 1 out of many functions that library offers, plus there no guarantee or knowing what the code really does if it is not documented properly.

That said - Monkey is really smart when it comes to imports. So importing the whole Diddy framework and only using RealMillisescs() should not give you any overhead - since only the code in that very function should be translated.

Sounds like it can't be done at the moment.

If you goal is to have said functionality, it is very doable.

1. Download Diddy into your module folder (takes you max 2 minutes)
2. Write "import Diddy" at top of the file/module where you want to use Realmillisecs()
3. Add "Real" infront of Millisecs() where desired

And what that has to do with only using mojo I do not get. What is interesting tough is why do you not want to use external libraries? (No offence meant, just curious really)


Grey Alien(Posted 2012) [#10]
Yes Diddy is very easy to plug in and use part of or all of. Only issue is adding to an existing project you could find naming conflicts.


Tibit(Posted 2012) [#11]
Grey Alien, any good design idea on how to avoid naming conflicts?

I have come to rely on the Alias keyword for this, but maybe not everyone knows how to best use that?

I know in Blitzmax I always added a prefix, but now days I rarely use "global" functions, and so clashes are less likely I guess.


Gerry Quinn(Posted 2012) [#12]
If RealMillisecs() is all I want I import diddy.functions, which contains only a few names that are unlikely to clash with anything.


therevills(Posted 2012) [#13]
Most of the time, you wont need to worry about naming conflicts and if one does pop up you will just to prefix which module name you want to use. For example:

Diddy:
[monkeycode]Global game:DiddyApp[/monkeycode]
Your code:
[monkeycode]Global game:MyGameApp[/monkeycode]
And when you use them:
[monkeycode]diddy.game = New DiddyApp
myfile.game = New MyGameApp[/monkeycode]
BTW I am thinking of cleaning up the functions file in the future... so watch out :P


Dima(Posted 2012) [#14]
I guess I meant "how can you do it with just monkey/mojo" --


How about storing millisecs using mojo's string saving capabilities, then seeding the random function by loading that value. Next value saved would be initial value + millisecs etc.

Basically it's kind of the same thing as it is now; the first execution would produce same results but every consecutive run would 'continue' the millisecs value from previous runs.

Obviously there are flaws. Losing the saved data would reset the seed back to the 'initial' run state. Also, you're still not seeding with a random value, but at least the numbers should be different from run to run.

I have not tested this but in theory I don't see why not? There might be more that can be done here - haven't had the time to think it over though.

Cheers,
Dmitriy


Gerry Quinn(Posted 2012) [#15]
Yes, Dmitri's solution is fine except for the issue of losing saved data, so long as the number of calls to Rnd() depends on user actions, as will be the case with most games.

But RealMillisecs() (or whatever its future replacement is!) works fine and is probably the simplest option.