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:
Generating random 32-bit value

Subject: Generating random 32-bit value

From: Philipp

Date: 12 Feb, 2009 19:25:04

Message: 1 of 4

Hi

Very simple problem: I would like to create a random 32-bit value represented
in HEX. I played a little bit around with the rand function and like this I can easily
generate a random value:

dec2hex(floor(2^32 * rand));

However, I would be interested how "random" this numbers are? Is there some bias?
Can I somehow set a seed when using rand?

Many thanks

Subject: Generating random 32-bit value

From: Peter Perkins

Date: 12 Feb, 2009 20:10:32

Message: 2 of 4

Philipp wrote:

> Very simple problem: I would like to create a random 32-bit value represented
> in HEX. I played a little bit around with the rand function and like this I can easily
> generate a random value:
>
> dec2hex(floor(2^32 * rand));

The way you've stated this question makes me think that you somehow want to use the individual bits, and that's not really what the generators in MATLAB are for. The fundamental thing in MATLAB is generating U(0,1) values, and anything else is a conversion from that. Some of the generators create random 32bit integers under the hood, but not all of them.

In the most recent version,

   randi([0,2^32-1],'uint32')

plus dec2hex, I guess, will formally give you what you've asked for, but since you haven't said what you're doing with it, it's hard to know if you're getting what you really want, as I said. In earlier versions, your code is the equivalent thing.

> However, I would be interested how "random" this numbers are? Is there some bias?

The values you get from randi([0,2^32-1],'uint32') are uniform on the set of values [0,2^32-1]. Ditto your code.

> Can I somehow set a seed when using rand?

Read the help for RAND, paying particular attention to things like:

 "Resetting the default stream to the same fixed state allows computations
  to be repeated. Setting the stream to different states leads to unique
  computations, however, it does not improve any statistical properties."

Hope this helps.

Subject: Generating random 32-bit value

From: Roger Stafford

Date: 13 Feb, 2009 02:01:02

Message: 3 of 4

"Philipp " <Patrick.Bateman23@gmx.at> wrote in message <gn1t2g$a4s$1@fred.mathworks.com>...
> Hi
>
> Very simple problem: I would like to create a random 32-bit value represented
> in HEX. I played a little bit around with the rand function and like this I can easily
> generate a random value:
>
> dec2hex(floor(2^32 * rand));
>
> However, I would be interested how "random" this numbers are? Is there some bias?
> Can I somehow set a seed when using rand?
>
> Many thanks

  Peter makes a good point, Kyle. Even if 'rand' gives a reasonably good statistically uniform distribution on the interval [0,1] and has other good statistical properties, that doesn't mean this good behavior extends equally to its individual bits down toward the less significant end. Perhaps there is some undesirable pattern that could be discerned statistically in the 32nd bit down. (Perhaps an encoded commercial message from MathWorks? I'm just kidding! :-) .)

  Of course you can always get around this if you are willing to call on 'rand' more frequently than once in generating each 32-bit integer. In the extreme you could call on it 32 times for each integer, as in:

 x = round(rand(n,32))*2.^(0:31).';

which would create an n-element column vector of 32-bit unsigned integers whose individual bits are statistically of good quality, I would think. (The bits here are computed backwards but that shouldn't make any difference.)

Roger Stafford

Subject: Generating random 32-bit value

From: Peter Perkins

Date: 13 Feb, 2009 14:39:54

Message: 4 of 4

Roger Stafford wrote:

> Peter makes a good point, Kyle. Even if 'rand' gives a reasonably good statistically uniform distribution on the interval [0,1] and has other good statistical properties, that doesn't mean this good behavior extends equally to its individual bits down toward the less significant end. Perhaps there is some undesirable pattern that could be discerned statistically in the 32nd bit down. (Perhaps an encoded commercial message from MathWorks? I'm just kidding! :-) .)

Some people believe that if you use the secretDecoderRing method of the new RandStream class, the lowest order bit ends up spelling out, "Be sure to drink your Ovaltine." There's no truth to that, however.

Tags for this Thread

No tags are associated with 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