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

Username:   Password: 
 RegisterRegister   
 random real numbers
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DJ




PostPosted: Thu Aug 07, 2003 3:35 pm   Post subject: random real numbers

i wanna make random real numbers between a certain range.
i wanna make these real numbers prices, so ineed a decimal cutoff
how?
Sponsor
Sponsor
Sponsor
sponsor
poly




PostPosted: Thu Aug 07, 2003 5:27 pm   Post subject: (No subject)

ooops what I posted here was for integers, sorry!
AsianSensation




PostPosted: Thu Aug 07, 2003 7:04 pm   Post subject: (No subject)

Rand.Real

it generates a real number between 0 and 1

then just multiply it by a multiplier if you want bigger numbers to be generated

for example, you want a real number between 0 and 100

num:=Rand.Real * 100
DJ




PostPosted: Thu Aug 07, 2003 9:54 pm   Post subject: (No subject)

but if i want a random real number between 1 and 100 and with 2 decimal places?
i dunno how to use the round function, the help thing dont help
kmd-10




PostPosted: Thu Aug 07, 2003 10:00 pm   Post subject: (No subject)

ok .. use Rand.Real, giving you a number between 0 and 1.
then use a multiplier.
then multiply it by 100.
then use round(n).
then divide by 100, and throw in your dollar sign.

code:

var num : real
var int_num : int
var multiplier : int := 100 % now it's a number from 0 to 100.

num := Rand.Real * multiplier * 100
int_num := round(num)
num := int_num / 100



you could shorten this, but it's to show you what i mean.

code:

var multiplier : int := 10000
var num : real := (round(Rand.Real * multiplier)) / 100



and then after that, you'd need to do your check to add necessary zeroes. but that shouldn't be a problem.
DJ




PostPosted: Sun Aug 10, 2003 10:03 pm   Post subject: (No subject)

wow and then some that was a good answer
Namis




PostPosted: Thu Sep 11, 2003 3:06 pm   Post subject: Rand.Real

Couldnt you just use

randint(X,X) ???
krishon




PostPosted: Thu Sep 11, 2003 4:25 pm   Post subject: (No subject)

i think that's just for integers....
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Sep 11, 2003 4:44 pm   Post subject: (No subject)

it is... Rand.Int() actually uses Rand.Real() function inside itself to come up with an int.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
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  [ 9 Posts ]
Jump to:   


Style:  
Search: