Jump to content

Randomize name attribute


Dag

Recommended Posts

Hi,

I need to randomize name attribute and have a dict of names wich I have to randomize with.

Dict is as rob, car, ed.

The way I wanted to do it is like this but it does not work

if(name == "name", switch(int(rand($PT)*3), 0, "rob", 1, "car", 2, "ed"), "don")

How can I get a result I want?

Thanks!

D.

Edited by Dag
Link to comment
Share on other sites

Hi,

You want to start with an array of names

string names[] = {"rob", "car", "ed"};

Then, you want a random number, which goes from zero to the amount of items in the array

float random_value = rand(@ptnum) * len(names);

You need an integer, so you can floor() the result

int random_id = floor(random_value);

This could be combined with the previous line, like so

int random_id = floor(rand(@ptnum) * len(names));

Then, you can fetch the corresponding index from your names array

string picked_name = names[random_id];

In the end, this is the result

string names[] = {"rob", "car", "ed"}; // Array of names
int random_id = floor(rand(@ptnum) * len(names)); // Random ID, from zero to the amount of items in the array
string picked_name = names[random_id]; // The name corresponding to the random ID

 

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...