# Having trouble with randomisation

**URL:** <https://rubytalk.org/t/having-trouble-with-randomisation/61125>\
**Category:** ruby-talk\
**Created:** [12 December 2010 17:55 UTC](https://rubytalk.org/t/having-trouble-with-randomisation/61125 "2010-12-12T17:55:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deza\_Awesome](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@Deza\_Awesome](https://rubytalk.org/u/Deza_Awesome)\
**Post date:** [12 December 2010 17:55 UTC](https://rubytalk.org/t/having-trouble-with-randomisation/61125/1 "2010-12-12T17:55:31Z")

</div>

I want my program to say a diffrent maths question everytime the user  
goes through the cycle, however it just does the same one ☹ What can I  
do to achive my goal?

V2 = 1 + rand(22)

V1 = 1 + rand(22)

class Die

&nbsp;&nbsp;def roll  
&nbsp;&nbsp;&nbsp;&nbsp;1 + rand(22)  
&nbsp;&nbsp;end

end

def hello  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'whats your name'  
&nbsp;&nbsp;&nbsp;&nbsp;name = gets.chomp  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Hello ". + name  
&nbsp;&nbsp;end

def maths\_question  
&nbsp;&nbsp;puts 'Whats the sum of ' + V2.to\_s + ' and ' + V1.to\_s + ' ?'  
&nbsp;&nbsp;youSAY = gets.chomp  
&nbsp;&nbsp;while youSAY != CorrectA.to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'Wrong, please try again'  
&nbsp;&nbsp;&nbsp;&nbsp;youSAY = gets.chomp  
&nbsp;&nbsp;end  
end

def maths\_congrats  
&nbsp;&nbsp;puts 'Congrats, you got ' + CorrectA.to\_s + ', which was the correct  
answer!'  
end

hello  
puts 'Do you want to do maths questions?'  
domath = gets.chomp  
while domath == 'yes'

CorrectA = V1.to\_i + V2.to\_i

maths\_question  
maths\_congrats  
puts 'Do you want to do more?'  
domath = gets.chomp  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jeremy\_Bopp](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/jeremy_bopp/32/1937_2.png) [@Jeremy\_Bopp](https://rubytalk.org/u/Jeremy_Bopp)\
**Post date:** [12 December 2010 18:10 UTC](https://rubytalk.org/t/having-trouble-with-randomisation/61125/2 "2010-12-12T18:10:46Z")

</div>

V1 and V2 are constants calculated one time at the beginning of your  
script. If you want to loop through your logic with different values  
for V1 and V2 each time, you need to recompute them each time through  
your loop.

When you do that, you should name them with a lower case first letter so  
that ruby does not treat them as constants. Doing that will require  
that you rework your logic so that you can pass in these dynamically  
generated values to all of your methods that use them.

-Jeremy

> **···**
>
> On 12/12/2010 11:55 AM, Deza Awesome wrote:
> 
> > I want my program to say a diffrent maths question everytime the user  
> > goes through the cycle, however it just does the same one ☹ What can I  
> > do to achive my goal?
