What method to define a random float

F

Fan Jin

Hi:
I've applied the watir API to login to a website and input a integer
value into a textfield. According to my task is to test the website's
responding time regarding to distinguished values, it needs to input a
random number everytime. Thus, I've defined a random number by coding:

@num = rand(5)

This integer should not exceed than 5. However, I applied the Watir to
write the variable to that corresponding textfield where I coded:

$browser.text_field:)id, 'keyword_instance_SearchBidMax').set(@num)

#The variable @num should be any integer which is between 1 and 5.
However, it turned to the error message as following:

D:/ruby/lib/ruby/gems/1.8/gems/firewatir-1.6.7/lib/firewatir/elements/text_field.rb:199:in
`doKeyPress': undefined method `length' for 4:Fixnum (NoMethodError)

Can you help me to figure it out please? Thanks.
 
Z

Zhi-Qiang Lei

Hi,

When the argument to rand is 0, it will return a random float number =
greater than or equal to 0 and less than 1.

Hi:
I've applied the watir API to login to a website and input a integer
value into a textfield. According to my task is to test the website's
responding time regarding to distinguished values, it needs to input a
random number everytime. Thus, I've defined a random number by coding:
=20
@num =3D rand(5)
=20
This integer should not exceed than 5. However, I applied the Watir to
write the variable to that corresponding textfield where I coded:
=20
$browser.text_field:)id, 'keyword_instance_SearchBidMax').set(@num)
=20
#The variable @num should be any integer which is between 1 and 5.
However, it turned to the error message as following:
=20
= D:/ruby/lib/ruby/gems/1.8/gems/firewatir-1.6.7/lib/firewatir/elements/text=
_field.rb:199:in
`doKeyPress': undefined method `length' for 4:Fixnum (NoMethodError)
=20
Can you help me to figure it out please? Thanks.
=20
--=20
Posted via http://www.ruby-forum.com/.
=20


Best regards,
Zhi-Qiang Lei
(e-mail address removed)
 
J

Jeremy Bopp

Hi:
I've applied the watir API to login to a website and input a integer
value into a textfield. According to my task is to test the website's
responding time regarding to distinguished values, it needs to input a
random number everytime. Thus, I've defined a random number by coding:

@num = rand(5)

The subject of your message says that you want a random float rather
than a random integer. If you want a random float between 0 and 5, do
the following:

@num = rand * 5

Otherwise, you're doing fine with the method you're using to get a
random integer.
This integer should not exceed than 5. However, I applied the Watir to
write the variable to that corresponding textfield where I coded:

$browser.text_field:)id, 'keyword_instance_SearchBidMax').set(@num)

#The variable @num should be any integer which is between 1 and 5.
However, it turned to the error message as following:

D:/ruby/lib/ruby/gems/1.8/gems/firewatir-1.6.7/lib/firewatir/elements/text_field.rb:199:in
`doKeyPress': undefined method `length' for 4:Fixnum (NoMethodError)

You passed your integer to a method that expects a string. That's why
something is ultimately calling the length method on the object you
passed in. Look up the to_s method for the Integer class and see if
that helps you out.

-Jeremy
 
F

Fan Jin

Jeremy Bopp wrote in post #970763
The subject of your message says that you want a random float rather
than a random integer. If you want a random float between 0 and 5, do
the following:

@num = rand * 5


Thanks a lot. It works fine now :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top