Rand.Int
Author |
Message |
AK.E
|
Posted: 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.
Turing: | var randomNumber : int := 0
|
| % some code goes here
|
loop
randomNumber := Rand.Int (2. 14)
end loop
|
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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Teti
![](http://compsci.ca/v3/uploads/user_avatars/19903815645b786e0e4308.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
ericfourfour
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
agnivohneb
![](http://compsci.ca/v3/uploads/user_avatars/13633306154a6e6befc7b53.png)
|
Posted: Wed Jan 24, 2007 2:33 pm Post subject: Re: Rand.Int |
|
|
You can try something like this
code: | var randomNumber : int := 0
|
| % some code goes here
|
loop
randomNumber := Rand.Int (2, 36)
exit when randomNumber <= 14 or randomNumber >= 28
end loop |
I have a question for you how did you get it to say turing then write the code in the turing colors |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: 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]. |
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: 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:
code: |
if Rand.Int (0, 1) = 0 then
put Rand.Int (2, 14)
else
put Rand.Int (28, 36)
end if
|
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 ]. |
|
|
|
|
![](images/spacer.gif) |
|
|