Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: how can generate random integer numbers?
Replies: 8   Last Post: Mar 19, 2013 6:02 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Nasser Abbasi

Posts: 5,688
Registered: 2/7/05
Re: how can generate random integer numbers?
Posted: Nov 17, 2005 5:06 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply



"emanuele" <emanuelemignosa@yahoo.it> wrote in message
news:ef1bf39.-1@webx.raydaftYaTP...
> Hi all
> how can generate random integer numbers?
> Thanks in advantage Emanuele
>


First you need to decide on the range of random numbers. Then use
rand(1) to generate and floating random number between 0 and 1, then
map the generated number to the range you want, and then chop of the
fractional part.

for example, if you want a random interger between 0 and 100, then
something like could be a solution

>> round(rand(1)*100)

ans =

48

if you want random intergers between 0 and 1000, then

>> round(rand(1)*1000)

etc..

if you want a random number between values other than 0 and some
positive upper limit, then you need to use a slightly more advanced
maping than the above. But the idea is the same. You use the random
number generated by rand(1) and map that to whatever you want.

Nasser









Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.