I writing an application in AVR Studio 4 which generates random numbers and outputs them on a seven segment display. At the moment i am using a seed, the seed value then gets randomized and the value output. This method obviously produces the same random number sequence (and displays the same sequence) every time the program is run. Is there an alternate method i can use which does not use a seed and as such does not start the program with the same number each time, allowing for different random numbers.
Thanks
Each time the microcontroller starts up it is seeing exactly the same internal state as any other time it starts up. This means its output will always be the same regardless of any algorithm you might use.
The only way to get it to produce different behaviour is to somehow modify its state at startup by introducing some external information or by storing state between startups. Some ideas for how to do the first option might be to measure the duration of a user key press (if your system has buttons) or sensing a temperature or other external input and using this to seed the algorithm. However the simplest option is probably to just store a counter in EEPROM that is incremented after each startup and use this to generate the seed.
Related
I have define host traffic send interval in this way in my NED file and I expect to receive different number in every execution:
volatile double sendInterval #unit("s") = default(exponential(1s));
but it gives me same result in every execution, where is the problem?
There isn't any problem here. This is a feature. Any random number in OMNeT++ is actually a pseudo-random number.
And they really need to be deterministic, so every experiment can be exactly reproduced.
See the manual section: https://omnetpp.org/doc/omnetpp/manual/#sec:sim-lib:random-number-generators
Where it says:
Starting from the same seed, RNGs always produce the same sequence of random numbers. This is a useful property and of great importance, because it makes simulation runs repeatable.
To get different values, try setting a different seed-set for your configuration in the .ini file, or running multiple repetitions by adjusting the repeat option - each repetition automatically sets a different seed for the PRNGs.
Also see: https://omnetpp.org/doc/omnetpp/manual/#sec:config-sim:repeating-runs-with-different-seeds
I'm creating a game where I want to create random worlds and give the player the option of having the same world made again by entering the same seed...
So... How can I do this? It will generate the same set of numbers, but not the same every time you call for the random function...
Do I have to make function of sorts based on the seed manually?
In GameMaker Studio and GameMaker 8.1 there is a function:
random_set_seed(seed)
Here you can enter your seed.
Then random(x) gives the same n-th result per game run.
random(100)
randomize() // This doesn't change rand[0] or rand[1]
random_set_seed(20) // because of this
rand[0]=random(100)
rand[1]=random(100)
Randomizer functions either use mathematical formulas which seem to give out random numbers or just look at an already calculated array of seemingly random numbers and return them in order. In most languages, there is a function which will look at the computer's clock and will use this value as a first value for the mathematical formula or as a starting index in the pseudorandom array. In GameMaker, you can use randomize() to do that. Only call that once when the game launches and you'll get different results on every execution.
If you want the opposite, that is you want to be able to regenerate the same seemingly random sequence, you can instead set the seed manually with random_set_seed(value). The value passed needs to be a number. Then you can give that seed to the player.
So if you want to generate a random level on the first time and later be able to replay the same level, you need to do in order:
Call randomize to set a random seed.
Call random_get_seed which will return the current seed.
Call random and the likes to generate a level.
When the player wants to use the same seed as before, call random_set_seed and pass it the seed.
Warning: By using those functions, you are putting your trust in GM's randomizing functions. These are platform dependent, and the functions might change in a later version of GM. If you want your seeds to work across all platforms and versions of your game, you might want to work on your own randomizing functions.
For my SIMULINK model I need to generate a signal that takes the values 1 or 0. To generate it I need to draw a number from a an exponential distribution and use this number as the time the signal stays in 0. Once this time has passed, I have to draw a new number from the exponential distribution and use this number as the time the signal stays in 1, and the repeat the process until the end of the simulation. As a SIMULINK newbie I'm quite puzzled by this problem and would appreciate any suggestions on how to solve it.
You've got a couple of choices.
In MATLAB, you can generate all samples in advance (i.e. before running the simulation) and use them to create a suitable signal, then use that as an input into the model (using the From Workspace block).
Or, if you need to do the sampling at each time step, then you have to write an S-Function, using the random number in the mdlGetTimeOfNextVarHit method. There is an example of doing something very similar on the Goddard Consulting web site called Square Wave with Jitter.
I hope it's not too obvious a question: is there a random number generation algorithm that doesn't depend on previously returned values, so that I can get (for example) the 50th number in the sequence, without computing the previous 49?
The reason is that I am making roguelike that will be persistent (so that I can recreate the exact same level from the same seed), but to compute certain features of each level, I don't want to have to "compute" all previous features just to get the random number generator to the correct "state" of having been used, for example, 100 times so far. I would like to be able to query the 101st random number without determining previous values so that the program can create level features separately.
You can encrypt ordinary sequence number [1..N] with any cipher,
and by this way - generate unique pseudorandom value for each SeqNo.
If you use a linear congruential random number generator, it is trivial to compute the $n$-th element generated from a given seed. But it is probably easier just to stash away the state at the "interesting" points of the game.
OTOH, if you want to "restart" the game at a certain point, you'll presumably want to be able to recreate the dungeon's features, but (due to different player actions) the RNG usage will be different from then on. I.e., if started at the same point, if I shoot twice at a monster the RNG will be used more times than if I just run away; the next item generated will get different values. Perhaps what you really want is several independent random number streams, and saving the states as needed?
There are lots of roguelike games around, mostly open source. Some are limited/small (from "build a game in a day" sort of competitions), and might make a good starting point for you. Why start your own, and not hack on an existing one?
I read that seeds are used to initialize random number generators. But seems like the randomness of the seed doesn't matter much for getting good randomness from the generator. So I want to understand what is a seed actually? Why is it called so? And lastly why time in a computer system is used to generate such seeds?
A pseudo-random number generator produces a sequence of numbers. It isn't truly random, but generally a mathematical calculation which produces an output that matches some desirable distribution, and without obvious patterns. In order to produce such a sequence, there must be state stored for the generator to be able to generate the next number in that sequence. The state is updated each time using some part of the output from the previous step.
Seeding explicitly initialises this state. A 'seed' is a starting point, from which something grows. In this case, a sequence of numbers.
This can be used either to always generate the same sequence (by using a known constant seed), which is useful for having deterministic behaviour. This is good for debugging, for some network applications, cryptography, etc.
Or, in situations where you want the behaviour to be unpredictable (always different each time you run a program, a card game perhaps), you can seed with a number likely to be continually changing, such as time.
The 'randomness' of the sequence does not depend on the seed chosen, though it does depend on not reseeding the sequence.