# Random method

**URL:** <https://www.ruby-forum.com/t/random-method/153633>\
**Category:** Rails\
**Created:** [November 19, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/random-method/153633 "2008-11-19T17:13:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amarime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@amarime](https://www.ruby-forum.com/u/amarime)\
**Post date:** [November 19, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/random-method/153633/1 "2008-11-19T17:13:26Z")

</div>

hi group  
I’m new in RubyonRails world i need to define a method that will  
generate random CPU utilization (0-100%)

---

<div class="post-metadata">

**Author:** ![amarime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@amarime](https://www.ruby-forum.com/u/amarime)\
**Post date:** [November 19, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/random-method/153633/2 "2008-11-19T17:51:00Z")

</div>

On 19 Nov 2008, at 15:36, amarime wrote:

> hi group  
> I’m new in RubyonRails world i need to define a method that will  
> generate random CPU utilization (0-100%)

as in something that will create that amount of load on the computer  
it’s running on ?

Fred

---

<div class="post-metadata">

**Author:** ![amarime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@amarime](https://www.ruby-forum.com/u/amarime)\
**Post date:** [November 19, 2008, 5:59pm UTC](https://www.ruby-forum.com/t/random-method/153633/3 "2008-11-19T17:59:13Z")

</div>

no just by fake numbers that will be generated from the method inside  
the model  
On Nov 19, 11:42 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![amarime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@amarime](https://www.ruby-forum.com/u/amarime)\
**Post date:** [November 20, 2008, 9:45am UTC](https://www.ruby-forum.com/t/random-method/153633/4 "2008-11-20T09:45:04Z")

</div>

Two options:  
1+rand(100) will give you a range of 1-100 (it will never show as 0%)  
rand(101) will give you a range of 0-100 (0% is possible)

Basic ruby 101 to be honest.

On 19 Nov 2008, at 17:58, amarime wrote:

> > > generate random CPU utilization (0-100%)
> > 
> > as in something that will create that amount of load on the computer  
> > it’s running on ?

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![amarime](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbce88/32.png) [@amarime](https://www.ruby-forum.com/u/amarime)\
**Post date:** [November 20, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/random-method/153633/5 "2008-11-20T18:11:12Z")

</div>

> 1+rand(100) will give you a range of 1-100 (it will never show as 0%)  
> rand(101) will give you a range of 0-100 (0% is possible)

I don’t know what your actually trying to accomplish, but it doesn’t  
seem to me that simply generating random numbers between 0 and 100 would  
be a very realistic test of CPU load. Load on a CPU wouldn’t look like  
random noise.

I think I would generate random numbers that add to another value in  
small increments in some form of moving average to produce a more  
realistic result. But, this depends on if you care about the value over  
time, or just need an instance in time value.
