LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

generate random number

Solved!
Go to solution

Hi everyone, 

I'm trying to make a VI that generates a random number (from 0 to 10 per example), but never generates a same number and stops when all the numbers have been generated. I don't know if this is clear enough.

0 Kudos
Message 1 of 19
(7,935 Views)

Are you asking a question about your code?

 

Is this a homework assignment?

 

The use of 1 divided by  (there is a 1/x reciprocal function on the palette, by the way) and the quotient remainder is a very odd way to generate a random number.  Usually you just multiply the random number by the number of different values you want, then round down.  So to get 0 to 10,  multiply by 11 and round down.

 

I don't see any loop in your VI, so this is only going to execute once.

0 Kudos
Message 2 of 19
(7,930 Views)

Something like this:

RandomNumbers.PNG

 

Note that, very unlikely, it is possible that you end up with one 11 in the dataset.....

 

EDIT: Additionally, the other case(s) of the case structure are homework 🙂

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 3 of 19
(7,924 Views)

@Norbert_B wrote:

Note that, very unlikely, it is possible that you end up with one 11 in the dataset.....

 

EDIT: Additionally, the other case(s) of the case structure are homework 🙂


Actually, I don't think you can get an 11.  You'd have to get a 1 out of the random number, but it is defined as 0 to 0.99999........  It includes 0 on the lower end, but does not include 1 on the upper end.

 

(Read the context help.  Smiley Wink)

0 Kudos
Message 4 of 19
(7,915 Views)

@RavensFan wrote:
[...]You'd have to get a 1 out of the random number, but it is defined as 0 to 0.99999........  It includes 0 on the lower end, but does not include 1 on the upper end.

 

(Read the context help.  Smiley Wink)


That is right. However, i think that in older versions of LV the help was not that accurate (as i doubt that RandomNumber has been changed function wise).

Well, anyhow, that makes my code work 100%, so it is good news for me 🙂

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 5 of 19
(7,899 Views)

One simple solution would be to use the Ramp function to create an array of numbers from 0 to 10 and then the Riffle function.  The Riffle just randomizes the order in the array.  There is also an example floating around where you could just use the Initialize Array to create an array with 11 elements (value does not matter here) and use the Index output of the Riffle as your random numbers.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 6 of 19
(7,878 Views)

@crossrulz wrote:

One simple solution would be to use the Ramp function to create an array of numbers from 0 to 10 and then the Riffle function.


Assuming this actually is a homework problem... that would be a very interesting solution.  If someone handed that to me I don't know if I'd be mad because they obviously didn't do the assignment as I intended, or impressed that they came up with the workaround

0 Kudos
Message 7 of 19
(7,869 Views)

Without a while loop:

ran.png

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 8 of 19
(7,847 Views)

Can you please send me the VI? 

0 Kudos
Message 9 of 19
(7,814 Views)

Quick tip: if you can see a dashed border around a LabVIEW screenshot (like paul_cardinale's one above), it is a 'code snippet' and you can drag and drop the code directly onto a block diagram. See here for more info.

0 Kudos
Message 10 of 19
(7,807 Views)