RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
Assigning a value to a variable

0 Members and 1 Guest are viewing this topic.

**
Rep:
Level 87
Single eared prophet
I want to have 2 variables which point on a random number between 1 and 10, but the 2 variables must point on different values, I don’t want both of them to value 5 or another value at the same time. So I wrote this method:
Code: [Select]
def give_a_value
@num_array.each do |num|
value=1+rand(10)
if value==num
give_a_value
end
end
@num_array.push(value)
value
end

@num_array=[0]
var1=give_a_value
var2=give_a_value
The problem is that it doesn’t work. It seems like the program doesn’t enter the loop ôO. I’ve been thinking about this for some days and I still don’t find the solution, so I ask for your help.
« Last Edit: April 05, 2007, 11:27:54 AM by Samael »

***
Rep:
Level 90
Try this:  ;)
Code: [Select]
var1 = rand(10) + 1
var2 = var1
while var1 == var2
  var2 = rand(10) + 1
end

**
Rep:
Level 87
Single eared prophet
If it was that simple I would already have found the way ^^. But thank you for having tried.
Actually I forgot to mention that I need to do this for 8 variables (and even more if I decide to increase the values that the variable can take and by the way the number of variables... in another code I'll need it...), by doing like that my code would be way too huge. That's why I need to use a method.

***
Rep:
Level 90
Try this:
Code: [Select]
vars = Array.new(8, 0)
vars.each_index do |i|
  a = 0
  while vars.include?(a)
    a = rand(10) + 1
  end
  vars[i] = a
end

**
Rep:
Level 87
Single eared prophet
That could work, thanks ^^, I didn't already try to puts all my variables into an array. I'm going to test it to see if it doesn't interfere with the rest.