NI Home > Community > NI Discussion Forums

LabVIEW

Showing results for 
Search instead for 
Do you mean 
Reply
Member
Laudie3a
Posts: 27
0 Kudos
Accepted Solution

How do I generate a random integer from 1-5?

 If I use random how do I convert it to an integer?
Active Participant
Guruthilak
Posts: 469
0 Kudos

Re: How do I generate a random integer from 1-5?

check out the conversion palette under "Numeric->conversion"

 

untitled.GIF

Regards
Guru (CLA)
Member
Pattos
Posts: 77
0 Kudos

Re: How do I generate a random integer from 1-5?

[ Edited ]

For a Random Interger from 1 to 5 you do something like this:

 

1-5 Random.JPG

Edit: I just realised, that the chances for every Number are not the same, so my version is kind of unusable for statistically correct purposes.

Its too early in the morning :smileywink:

Edit2: Also I realise further, that my version won't output 1-5 but 0-5 so it is not even what you wanted. D**n what did I do :smileyvery-happy:

 

Cheers

Message Edited by Pattos on 01-20-2010 01:17 AM
Message Edited by Pattos on 01-20-2010 01:21 AM
Pattos

LabVIEW Gretchin
Member
Laudie3a
Posts: 27
0 Kudos

Re: How do I generate a random integer from 1-5?

Could you just use a +1 increment after the multiplication? What does the I32 do exactly?
Knight of NI
altenbach
Posts: 27,634

Re: How do I generate a random integer from 1-5?

[ Edited ]

Alana wrote:
What does the I32 do exactly?

"To I32" converts your floating point number to an integer, as you specified in your first post as requirement. :smileywink:

 

Here' a version that gives all 5 numbers with equal probability. Instead of explicitely converting to I32, I use the output configuration of the +1. Same difference. :smileywink:

 

 

Message Edited by altenbach on 01-19-2010 11:39 PM

LabVIEW Champion . Do more with less code and in less time .

Trusted Enthusiast
ian_fung
Posts: 2,609
0 Kudos

Re: How do I generate a random integer from 1-5?

dear altenbach,

 

just some "additional" inputs to make it configurable... please kindly comment and advise. Thanks!

 

LVVILIB_NUM_integer random number generation.PNG  

Ian F
Since LabVIEW 5.1... 7.1.1... 2009, 2010
依恩与LabVIEW
LVVILIB.blogspot.com
Trusted Enthusiast
ian_fung
Posts: 2,609
0 Kudos

Re: How do I generate a random integer from 1-5?

A similar one here 

Ian F
Since LabVIEW 5.1... 7.1.1... 2009, 2010
依恩与LabVIEW
LVVILIB.blogspot.com
Trusted Enthusiast
aeastet
Posts: 2,199
0 Kudos

Re: How do I generate a random integer from 1-5?

Wouldn't you have to multiply times 4 vs 5. If the random number give you a one then you would get 5 and add 1 to it giving you 6.

 

 

Example.png

Tim
Johnson Controls
Holland Michigan
Trusted Enthusiast
aeastet
Posts: 2,199
0 Kudos

Re: How do I generate a random integer from 1-5?

Wouldn't you have to multiply times 4 vs 5. If the random number give you a one then you would get 5 and add 1 to it giving you 6.

 

 

Example.png

Tim
Johnson Controls
Holland Michigan
Member
Pattos
Posts: 77
0 Kudos

Re: How do I generate a random integer from 1-5?

No aestet, Altenbachs solution is perfectly fine.

 

The dice generate a Number between 0 and 1 (Not including 1!!) Multiplied by 5 that is a Number between 0 and 4.999999.

The next triangle always rounds the Number down, so that you can only get 0,1,2,3 and 4 everything with equal propablity.

Plus one you get 1,2,3,4 and 5. 

 

Pattos

LabVIEW Gretchin