Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Random, Random Numbers

Subject: Random, Random Numbers

From: Richard

Date: 15 Mar, 2009 10:36:01

Message: 1 of 13

Morning everyone!

Is there a method for making MATLAB's rand function actually produce random numbers? At the moment, each time I start MATLAB up, it produces the same random nubmer sequence and I could really do with different numbers being generated each time.

Many Thanks,

Rich :o)

Subject: Random, Random Numbers

From: us

Date: 15 Mar, 2009 10:45:03

Message: 2 of 13

"Richard"
> Is there a method for making MATLAB's rand function actually produce random numbers? At the moment, each time I start MATLAB up, it produces the same random nubmer sequence and I could really do with different numbers being generated each time...

carefully read the doc again to see the solutions...

us

Subject: Random, Random Numbers

From: Sprinceana

Date: 15 Mar, 2009 15:16:01

Message: 3 of 13

"us " <us@neurol.unizh.ch> wrote in message <gpim7f$t4e$1@fred.mathworks.com>...
> "Richard"
> > Is there a method for making MATLAB's rand function actually produce random numbers? At the moment, each time I start MATLAB up, it produces the same random nubmer sequence and I could really do with different numbers being generated each time...
>
> carefully read the doc again to see the solutions...
>
> us

On your form you have to put a pushbutton component and an edit component. I made a simple application for generating random numbers in an edit and I want to share this with you.



function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

y=rand; %generation a random number using rand function on variable y
set(handles.edit1,'string',y); %put in edit1 the value generated by variable y

P.S Function rand only stores random values comprised in the interval [0,1]

The effect of this code is the following:

When you press the first time pushbutton1 looks like that:

http://img19.imageshack.us/img19/8725/randomh.png

When you press the second time pushbutton1 looks like that:

http://img19.imageshack.us/img19/8254/random1u.png

For further information doc rand in Command Window of Matlab

Subject: Random, Random Numbers

From: Sprinceana

Date: 15 Mar, 2009 15:16:01

Message: 4 of 13

"us " <us@neurol.unizh.ch> wrote in message <gpim7f$t4e$1@fred.mathworks.com>...
> "Richard"
> > Is there a method for making MATLAB's rand function actually produce random numbers? At the moment, each time I start MATLAB up, it produces the same random nubmer sequence and I could really do with different numbers being generated each time...
>
> carefully read the doc again to see the solutions...
>
> us

On your form you have to put a pushbutton component and an edit component. I made a simple application for generating random numbers in an edit and I want to share this with you.



function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

y=rand; %generation a random number using rand function on variable y
set(handles.edit1,'string',y); %put in edit1 the value generated by variable y

P.S Function rand only stores random values comprised in the interval [0,1]

The effect of this code is the following:

When you press the first time pushbutton1 looks like that:

http://img19.imageshack.us/img19/8725/randomh.png

When you press the second time pushbutton1 looks like that:

http://img19.imageshack.us/img19/8254/random1u.png

For further information doc rand in Command Window of Matlab

Subject: Random, Random Numbers

From: Sprinceana

Date: 15 Mar, 2009 15:20:02

Message: 5 of 13

"us " <us@neurol.unizh.ch> wrote in message <gpim7f$t4e$1@fred.mathworks.com>...
> "Richard"
> > Is there a method for making MATLAB's rand function actually produce random numbers? At the moment, each time I start MATLAB up, it produces the same random nubmer sequence and I could really do with different numbers being generated each time...
>
> carefully read the doc again to see the solutions...
>
> us

On your form you have a pushbutton and an edit.

Here is a little example that I made (I am beginner in matlab) hope that this helps u:

pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

y=rand; %generate random number in variable y
set(handles.edit1,'string',y); %store random number in edit

P.S rand function generates only numbers comprised in interval [0,1]

For further information type in Command Window :

doc rand

Subject: Random, Random Numbers

From: Sprinceana

Date: 15 Mar, 2009 15:20:02

Message: 6 of 13

"Richard " <r.j.macpherson@durham.ac.uk> wrote in message <gpilmh$rks$1@fred.mathworks.com>...
> Morning everyone!
>
> Is there a method for making MATLAB's rand function actually produce random numbers? At the moment, each time I start MATLAB up, it produces the same random nubmer sequence and I could really do with different numbers being generated each time.
>
> Many Thanks,
>
> Rich :o)


On your form you have a pushbutton and an edit.

Here is a little example that I made (I am beginner in matlab) hope that this helps u:

pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

y=rand; %generate random number in variable y
set(handles.edit1,'string',y); %store random number in edit

P.S rand function generates only numbers comprised in interval [0,1]

For further information type in Command Window :

doc rand

Subject: Random, Random Numbers

From: Sprinceana

Date: 15 Mar, 2009 15:20:03

Message: 7 of 13

"Richard " <r.j.macpherson@durham.ac.uk> wrote in message <gpilmh$rks$1@fred.mathworks.com>...
> Morning everyone!
>
> Is there a method for making MATLAB's rand function actually produce random numbers? At the moment, each time I start MATLAB up, it produces the same random nubmer sequence and I could really do with different numbers being generated each time.
>
> Many Thanks,
>
> Rich :o)


On your form you have a pushbutton and an edit.

Here is a little example that I made (I am beginner in matlab) hope that this helps u:

pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

y=rand; %generate random number in variable y
set(handles.edit1,'string',y); %store random number in edit

P.S rand function generates only numbers comprised in interval [0,1]

For further information type in Command Window :

doc rand

Subject: Random, Random Numbers

From: Richard

Date: 15 Mar, 2009 22:17:01

Message: 8 of 13

Many thanks for both the responses.

I am still slightly confused however. I understand using the function rand gives a random number within the range of 0 to 1. However I need MATLAB to generate different random numbers each time it starts.

In the MATLAB Help files it states "Because MATLAB resets the rand state at startup, rand generates the same sequence of numbers in each session unless you change the value of the state input."

However I am unsure how to implement this. Help would be greatly appriciated.

Many thanks

Subject: Random, Random Numbers

From: Peter Perkins

Date: 16 Mar, 2009 01:34:11

Message: 9 of 13

Richard wrote:
> Many thanks for both the responses.
>
> I am still slightly confused however. I understand using the function rand gives a random number within the range of 0 to 1. However I need MATLAB to generate different random numbers each time it starts.
>
> In the MATLAB Help files it states "Because MATLAB resets the rand state at startup, rand generates the same sequence of numbers in each session unless you change the value of the state input."

Richard, the right answer depends on what version you are running.

If you are running something older than R2008b, the answer is right there in the help for rand (and you'll want to do something similar with randn. Specifically

> help rand
[snip]
     Examples:
 
        Return RAND to its default initial state.
           rand('twister',5489)
 
        Initialize RAND to a different state each time.
           rand('twister',sum(100*clock))

If you are running R2008b or R2009a, there are major enhancements for random number generation. You can start with the documentation ("help RandStream"), or here:

<http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i/>

and (especially) here:

<http://blogs.mathworks.com/loren/2008/11/13/new-ways-with-random-numbers-part-ii/>

Hope this helps.

Subject: Random, Random Numbers

From: Walter Roberson

Date: 17 Mar, 2009 19:46:08

Message: 10 of 13

Richard wrote:

> Is there a method for making MATLAB's rand function actually produce random numbers?

No, there isn't. *All* of Matlab's rand() options over the years (including the
new randstream features of the newest versions) are pseudo-random number generators,
never true random number generators. You can access the pseudo-random stream at different
points by providing a different seed, but the question then becomes how to provide a
seed that is unique to that session. Unique could imply that -somehow- (through
some FEX function or other) you found a true random number -once- to use as the seed.

A lot of people seed using a hash (e.g., MD5) of the precise time mixed in with as
much unique information about the session as you can get ahold of (such as the
process identifier). But matlab doesn't provide any useful mechanism to get access
to the process identifier (unless it could be done via java). If you use the
time, even down to the millisecond, you run into the problem that two people
might start at the same time as known to their system (and if this involves running
on multiple computers, recall that it is virtually certain that one of the computers
will have its clock slow or fast relative to some of the other systems, leading
to the possibility that two different people on different systems could be told the
same time. And if you are attempting to use this random number system for security
functions, then it makes a difference: on most systems, it is not at all difficult
for people to write hardware or software to get the clock to report any specific
time they want.

If you need true random number generators (even if only to get the initial random seed)
then there are random number generators on the 'net that can be contacted via
urlread(); and I would think it likely that someone or other has already written
a FEX contribution to do the work for you.

Subject: Random, Random Numbers

From: Yair Altman

Date: 17 Mar, 2009 20:21:01

Message: 11 of 13

> ...But matlab doesn't provide any useful mechanism to get access
> to the process identifier (unless it could be done via java)...

java.lang.management.ManagementFactory.getRuntimeMXBean.getName.char

returns the Matlab process ID (well, actually the PID of its JVM but that's the same PID as Matlab's) coupled with the computer name, as so:
'4896@yair-altman'

Yair Altman

Subject: Random, Random Numbers

From: Stan Bischof

Date: 17 Mar, 2009 20:21:50

Message: 12 of 13

Walter Roberson <roberson@hushmail.com> wrote:
>
> If you need true random number generators (even if only to get the initial random seed)
> then there are random number generators on the 'net that can be contacted via
> urlread(); and I would think it likely that someone or other has already written
> a FEX contribution to do the work for you.
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Note that of course one cannot get true random numbers from software since software
does not have an underlying random process to draw from. Hence one must obtain
such from the "real world"- typically by measurements of some random physical
process.

So going to one of the web services which generates random numbers is
indeed a good way to go if you really need random numbers. Note that
this is not unique to Matlab but rather is inherent to software in general.

Stan

Subject: Random, Random Numbers

From: Peter Boettcher

Date: 17 Mar, 2009 20:28:45

Message: 13 of 13

Stan Bischof <stan@newserve.worldbadminton.com> writes:

> Walter Roberson <roberson@hushmail.com> wrote:
>>
>> If you need true random number generators (even if only to get the initial random seed)
>> then there are random number generators on the 'net that can be contacted via
>> urlread(); and I would think it likely that someone or other has already written
>> a FEX contribution to do the work for you.
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> Note that of course one cannot get true random numbers from software since software
> does not have an underlying random process to draw from. Hence one must obtain
> such from the "real world"- typically by measurements of some random physical
> process.
>
> So going to one of the web services which generates random numbers is
> indeed a good way to go if you really need random numbers. Note that
> this is not unique to Matlab but rather is inherent to software in general.

Some operating systems provide functionality to get true random numbers,
with entropy contributed by unpredictable environmental factors.

/dev/random on linux, for one

http://en.wikipedia.org/wiki/Urandom

-Peter

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us