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

Username:   Password: 
 RegisterRegister   
 Generating random numbers between inputed x and y - LOOP
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Loomis




PostPosted: Sun Oct 18, 2009 1:56 pm   Post subject: Generating random numbers between inputed x and y - LOOP

What is it you are trying to achieve?
I'm creating a program that generates and displays 10 random real numbers between x and y, where x and y are integers inputed by the user.

What is the problem you are having?
I have no clue how to generate this but i do know how to do it if the numbers were already given.

Describe what you have tried to solve this problem
For example, if the program was to outputs 10 random real numbers from 0 to 10 I would code it like this:

Turing:


for count : 1 .. 10
    rand (number)
    put number *10
end for



I also did some research and saw a coding for a program that outputed random numbers between x and y. I didn't understand it since it was done with "rand.real", which i haven't learned and would rather avoid using.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
I tried coding the program and this is what i have so far:

Turing:


var x, y : int
var number : real

put "please enter x : " ..
get x
put "Please enter y : " ..
get y
put ""

for count : 1 .. 10
    rand (number)
    put number *x+y
end for




Obviously this method is wrong since whatever integers I input, the random numbers outputed are completely off range.
Please specify what version of Turing you are using
Version 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Sun Oct 18, 2009 2:18 pm   Post subject: RE:Generating random numbers between inputed x and y - LOOP

You want the random number between the ranges of x=4 and y=6. The range between them is 1.
Rand() will only return a Real number between 0 and 1. We only want random numbers between 0 and 2. So we have to stretch the values or multiply it.

range = y-x
rand() * range


Then we bump up the values to where we want them. 0..2 --> 4..6

rand() * range + x

Thus Random Number in range between x and y =
rand() * (y-x) + x
Loomis




PostPosted: Sun Oct 18, 2009 2:30 pm   Post subject: Re: Generating random numbers between inputed x and y - LOOP

So this is what I need with your help ... and it WORKED! It outputs ANY given x and y inputs with the all the numbers in the right range.

Turing:


var x, y : int
var number : real

put "please enter x : " ..
get x
put "Please enter y : " ..
get y
put ""

for count : 1 .. 10
    rand (number)
    put number *(y-x)+x
end for



Thank you SO much. I completely understand! Very Happy
andrew.




PostPosted: Sun Oct 18, 2009 3:15 pm   Post subject: RE:Generating random numbers between inputed x and y - LOOP

Or if you want it to say 2 decimal places, you can do this:
Turing:
randint (number, x*10**2, y*10**2)
put number / 10**2


BTW, 10**2 = 10^2 in Turing. Change 2 to the number of desired decimal places.
Tony




PostPosted: Sun Oct 18, 2009 3:38 pm   Post subject: RE:Generating random numbers between inputed x and y - LOOP

Turing:

put Rand.Int(x*10**2, y*10**2) / 10**2
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
andrew.




PostPosted: Sun Oct 18, 2009 3:59 pm   Post subject: RE:Generating random numbers between inputed x and y - LOOP

I was going to do that, but I thought that it would be more helpful as a variable. That way they can use it for something else.
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  [ 6 Posts ]
Jump to:   


Style:  
Search: