Computer Science Canada Rand.Int |
Author: | AK.E [ Wed Jan 24, 2007 1:41 pm ] | ||
Post subject: | Rand.Int | ||
In my program I've been trying to get one variable to generate a number any from 2-14 and 28-36.
I also want the variable to generate anything from 28 - 36. Is there a way to do this? I don't want any numbers from 15 - 27. If there is more information that you need just ask. |
Author: | Teti [ Wed Jan 24, 2007 1:53 pm ] |
Post subject: | Re: Rand.Int |
Its hard to help without more code to see how it will work with your program. |
Author: | ericfourfour [ Wed Jan 24, 2007 2:10 pm ] |
Post subject: | RE:Rand.Int |
You can try getting a random number between 2 and 22. If it is greater than 14, add 14. I'm sure there is a formula you can use as well. |
Author: | agnivohneb [ Wed Jan 24, 2007 2:33 pm ] | ||
Post subject: | Re: Rand.Int | ||
You can try something like this
I have a question for you how did you get it to say turing then write the code in the turing colors |
Author: | [Gandalf] [ Wed Jan 24, 2007 5:15 pm ] |
Post subject: | Re: Rand.Int |
Or you could use two Rand.Int() calls, which would be slightly more efficient then the above while loop. Not a big deal though. agnivohneb, use [syntax="Turing"]code goes here[/syntax]. |
Author: | Cervantes [ Wed Jan 24, 2007 5:17 pm ] |
Post subject: | RE:Rand.Int |
Use syntax tags. [ syntax="turing"] code [ /syntax] agnivohneb's method would work, but it's a bad way to do it. On a bad day, it could take a long time to generate a random number between 2 and 36 that is <= 14 or >= 28. It's possible we could just keep drawing numbers between 15 and 27 for a long time. ericfourfour's method, however, is good. We have to generate exactly one random number every time. No more. |
Author: | Cervantes [ Wed Jan 24, 2007 5:26 pm ] | ||
Post subject: | Re: Rand.Int | ||
Gandalf @ Wed Jan 24, 2007 5:15 pm wrote: Or you could use two Rand.Int() calls, which would be slightly more efficient then the above while loop. Not a big deal though.
2-14 has 13 possible values in the range, but 28-36 has 9 possible values. Thus, if you did something like this:
The chances of getting a 28 is higher than the chances of getting a 2. You'd have to do correction for that by changing the Rand.Int (0,1) at the start to be more like Rand.Int (1, 13+9) then checking whether that is <= 13 or not. So, yeah: that's needlessly complicated. ericfourfour's method. edit: [Gandalf], your name breaks the quote system. It's the use of [ and ]. |