Random number generator inside transformation

Chetan Sharma asked on July 31, 2014 13:46

Hello All,

I wanted to generate inside transformation a suffix to add to my html. I wrote this method which is giving me a random number between 1-4. However, I was expecting that may be it will give me a random value for each iteration of the repeater. However, what is happening is that all items are taking same random no. Though, this is working for me, however, I would like to create more randomness.

<a class="ga-tracking" href="<%#Eval("NodeAliasPath")%>" category_tag="window_location" action_tag="<%#Eval("Heading")%>">

-<%#GetRandomNumber()%>">
<%#Eval("Heading")%>

Any suggestions will be appreciated

Regards, Chetan

Recent Answers


Brenden Kehren answered on July 31, 2014 14:23

You might look at using the Random class.

Or if you don't want to use the Random class you could take the NodeID + DocumentID x NodeOrder and generate a somewhat random number.

Of if you don't need a number, you could use the NodeGuid for that particular node.

0 votesVote for this answer Mark as a Correct answer

Yehuda Lando answered on July 31, 2014 21:12

Did you write a macro method for that? That should work.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on August 1, 2014 03:22

Thanks Brenden,

However what I need is a random no between 1 and 3. I am showing few default images in the footer section of the website and hover image. I want to make this experience little random. So somehow if I can make the above have more randomness then things should be fine for me. I have my css of the type css-1, css-1:hover. So this much randomness will be enough for me.

Regards, Chetan

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on August 1, 2014 14:59

Use the Random Repeater webpart then. Set your path to whereever you have your images in the content tree and simply leave the transformation as is and let the webpart do the work. One thing to note is caching with this webpart. Make sure in the Performance section of the webpart you set the Partial cache minutes to 0. This way whenever you refresh the page, it will show a new image.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.