random in MicroPython does appear to do anything? - random

Using pybricks-micropython
Running this command to get a random port number, works under CPython.
import random
port = random.randint(50000,50999)
produces a number, only it is hardly random it is the same number each time I run the script. I am guessing MicroPython needs something more perhaps?
What am I missing?

John,
I looked up seed with random and used epoch time. Solved.
millis = int(round(time.time())
random.seed(millis)
port = random.randint(50000,50999)
Ok almost certainly produces a random you could predict, but hey this isn't for the lottery or anything, its for a port number :)
Thanks you, you seeded that answer, forgive the pun.

Related

In octave poissrnd ignores the seed that has been set

I am running octave 3.8.1. Even if I set the seed for the random number generator, poissrnd always produce a different number. Let us consider the following code, for example
for i=1:2
rand('state',1); randn('state',1);
poissrnd(10)
end
Running it in matlab, produce the same number in both iterations. Running it in Octave, always produce a different number.
How can I correctly set a seed to poissrnd?
Thank you
Ok, I found the solution. You have to use randp('state',1). Therefore, the script
for i=1:2
randp('state',1);
poissrnd(10)
end
would always produce the same numbers.

How does one seed the random number generator in Swift?

My app uses random numbers. I would like to seed the random number generator so that it won't be the same every time. How might I go about doing this?
EDIT:
What parameter do I give srand() to seed the random generator with the current time?
This works:
let time = UInt32(NSDate().timeIntervalSinceReferenceDate)
srand(time)
print("Random number: \(rand()%10)")

Random Seed in PIC18F

I'm going to run modified DES code(C language) on the PIC18F2550 microcontroller.
For this I am using mplabx IDE v 2 and Mplab xc8 v 1.30.
To modify the code, I need a random number so that each run will produce different numbers.
I want to use the rand function but I need a good seed for Srand function!
Good seed can be time, but since there is no such thing as a micro or I do not know!!
You can store an integer value in EEPROM. When the device boots, you use it as a seed and then increment and store it again so that at every reboot you will have a different seed, producing a different sequence for each run. That should be enough for what you want.
If you need something a little more sofisticated, you can try this 555+ADC random seed circuit.

Why won't randomization work (MATLAB)?

Okay, this is like the 5th time I have had to ask this question, and still nobody has been able to give me an answer or solution. But here we go again ...
I want to run a very simple little MATLAB program. All it does is RANDOMLY display images from a directory. Here is my code:
files = dir(fullfile(matlabroot,'toolbox','semjudge',bpic,'*.png'));
nFiles = numel(files);
combos = nchoosek(1:nFiles, 2);
index = combos(randperm(size(combos, 1)), :);
picture1 = files(index(nRep,1)).name;
picture2 = files(index(nRep,2)).name;
image1 = fullfile(matlabroot,'toolbox','semjudge',bpic,picture1);
image2 = fullfile(matlabroot,'toolbox','semjudge',bpic,picture2);
subplot(1,2,1); imshow(image1);
subplot(1,2,2); imshow(image2);
I have tried several different iterations of this, including replacing "nchoosek" with "randsample."
But it doesn't work! Every time I run the program, the script runs the same image files in the same order. Why is it doing this? It's like it randomized the image files the first time I ran it, but now it only runs them in that order, instead of randomizing them every time the script is run.
Can somebody please help me with this?
The pseudo-random number generator starts off from a specific seed. The "random" numbers provided are deterministic. You need to change the seed to change these numbers.
The benefit of this is that even if you use pseudo-randomness in your algorithm, you can always replay a run by using the same seed again.
Reference: http://www.mathworks.de/help/techdoc/ref/rng.html
As an elaboration of #ypnos's answer, you probably want to add a line like this:
rng('shuffle');
To the start of your code. That will seed the random number generator with a value based on the time, and then you should get a different sequence of random numbers.

When to stop the looping in random number generators?

I'm not sure StackOverflow is the right place to ask this question, because this question is half-programming and half-mathematics. And also really sorry if my question is stupid ^_^
I'm studying about Monte Carlo simulations via the "Monte Carlo Methods" book. One of the first thing I must learn is about Random Number Generator. The basic algorithm of RNG is:
1. Initialize: Draw the seed S0 from the distribution µ on S. Set t = 1.
2. Transition: Set St = f(St−1).
3. Output: Set Ut = g(St).
4. Repeat: Set t = t+ 1 and return to Step 2.
(µ is a probability distribution on the finite set of states S, the input is S0 and the random number we desire it the output Ut)
It is not hard to understand, but the problem here is I don't see the random factor which lie in the number of repeat. How can we decide when to stop the loop of the RNG? All examples I read which implement a RNG are loop for 100 times, and they returns the same value for a specific seed. It is not random at all >_<
Can someone explain what I'm missing here? Any help will be appreciated. Thanks everyone
You can't get a true sequence of random numbers on a computer, without specialized hardware. (Such specialized hardware performs the equivalent of an initial roll of the dice using physics to provide the randomness. Electronic ones often use the electronic noise of specialized diodes at constant temperatures; others use radioactive decay events.)
Without that specialized hardware, what you can generate are pseudorandom numbers which, as you've observed, always generate the same sequence of numbers for the same initial seed. For simple applications, you can often get away with generating an initial seed from the time of invocation, which is effectively random.
And when I say "simple applications," I am excluding cryptography. (Not just that, but especially that.)
Sometimes when you are trying to debug a simulation, you actually want to have a reproducible stream of "random" numbers so you might specifically sent a stream to start with a specific seed.
For instance in the answer Creating a facet_wrap plot with ggplot2 with different annotations in each plot rcs starts the answer by creating a reproducible set of data using the R code
set.seed(1)
df <- data.frame(x=rnorm(300), y=rnorm(300), cl=gl(3,100)) # create test data
before going on to demonstrate how to answer the actual question.

Resources