ID:1648683
 
(See the best response by GhostAnime.)
Code:


Problem description:Question how would i make a choice random using the rand proc like a rand then with a if statement like rand(1,2,3) then a if statement after

Best response
Honestly, you could have tried doing it yourself and received the answer through a very easy trial & error.

Here's how:
- rand() RETURNS a value (like how an input() returns a value the user entered)

- if() is used to check a condition.

Here are some variations
if(rand(Min, Max) == Value)  // Specific value
if(rand(Low, High) in 1 to 10) // if the rand() value is within 1..10


If you are trying to do a probability, you are better off using prob(), ex:
if(prob(50))  //  if 50% chance was successful - equivalent to if(rand(1,2) == 1)

if(prob(10)) // if 10% chance was successful - equivalent to if(rand(1,10) == 1) or if(rand(1,100) in 1 to 10)
and is it possible to put more than 1 rand like rand(1,2,3,4)
In response to YoungJR1232
YoungJR1232 wrote:
and is it possible to put more than 1 rand like rand(1,2,3,4)

You can only have two in rand(var1, var2)
In response to YoungJR1232
If you're looking to get a random choice from a list, take a look at the pick() proc.
In response to Mightymo
Mightymo wrote:
If you're looking to get a random choice from a list, take a look at the pick() proc.
yes so how would use do that in a if statement so would it work like the rand proc if(pick(jack,dog,cat) == dog)

proc
someproc()
var/list/l = list(one,two,three)
var/a = pick(l)
switch(a)
if(one)
world << "One"
if(two)
world << "Two"
if(three)
world << "Three"

Give switch a try if you want to pick stuff from a list.

It's not right for every situation, but it is incredibly useful.

Also incredibly useful is remembering that the input window here will automatically post stuff if you press tab and enter.
You'd probably want the pick to choose from a list of strings.

So:

if(pick("jack", "dog", "cat") == "dog")


But, that's probably not what you actually want to do.

Are you trying to do something unique for each possible choice, or just one choice?
ok ty i understand now
Yes, that would work. However, I cannot really see a use in using it like that. In an instance like that, using rand from 1 to 3 looking for a value of 2 would be equivalent. Generally, you use pick() if you need the result later, e.g. doing an action specific to the choice, and then incrementing the variable chosen. In such an instance, you would be better off placing the result of pick() into a variable, and then checking against that variable.

Edit: I'm slow.
ok im going to try this out
btw im trying to make it so when a player logs in it runs a proc to pick a class and i was thinking i could use rand (but now i guess pick) to randomly pick a class so if dog then your class is blah blah if cat your class is blah blah what Mada said is most suited
mob
verb
APickOfDick()
var/list/l = list("dog","cat","bear")
var/b = pick(l)
switch(b)
if("dog")
world << "Hello"
if("cat")
world << "what uo"
if("bear")
world << "Yooo"
it doesnt seem to be working
In response to YoungJR1232
YoungJR1232 wrote:
btw im trying to make it so when a player logs in it runs a proc to pick a class and i was thinking i could use rand (but now i guess pick) to randomly pick a class so if dog then your class is blah blah if cat your class is blah blah what Mada said is most suited

It would have been ideal to mention this at the beginning so we could have pointed this out to you much earlier. As we tell people, more information given is better (in most cases).
problem fixed it was a interface thing that i forgot to code in
In response to YoungJR1232
Everything appears correctly; do you have an interface file made? If so, make sure you enabled 'default' on your main output element so these messages can go to that output without specifying via output().

Personally, I would do
var/b = pick(list(...))
switch(b)
if you were not planning to use L again and needed b for another ref.
In response to GhostAnime
GhostAnime wrote:
YoungJR1232 wrote:
btw im trying to make it so when a player logs in it runs a proc to pick a class and i was thinking i could use rand (but now i guess pick) to randomly pick a class so if dog then your class is blah blah if cat your class is blah blah what Mada said is most suited

It would have been ideal to mention this at the beginning so we could have pointed this out to you much earlier. As we tell people, more information given is better (in most cases).
Yeah sorry my fault but thank you guys alot for your help now ik what i can do and i have a new proc to use