Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Rand.Int
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AK.E




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Teti




PostPosted: 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.
ericfourfour




PostPosted: 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.
agnivohneb




PostPosted: 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
[Gandalf]




PostPosted: 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].
Cervantes




PostPosted: 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.
Cervantes




PostPosted: 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 ].
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: