Thanks:  0
Likes:  0
Dislikes:  0

# Thread: Randomize Odd or Even

1. ## Randomize Odd or Even

Friends,
I have a quick question. I need a returned value odd or even 1 or 2. I have two bits of code and want the system to pick one or the other.

Randomize
randomNum = (Rnd(1-2))

In short if you double click on the object or if you click once and then click OK the same action is taken and I want to cover both by the system picking.
The code bit randomNum is always something like 0.220077 and never 1.002293.
Thanks, for any pointers.
BMD
PS. This is VBScript.

2. ## Re: Randomize Odd or Even

psuedo code for choosing 1 or 2:

- multiple your randomNum by 1000000 to convert to integer
- check if that result is divisible by 2 (modulo == 0)
- if yes, the random number is even, set your value to 2
- if no, the random number is odd, set it to 1

implementing in VBScript is an exercise for you.

3. ## Re: Randomize Odd or Even

Thanks Corey,
By multiplying randomNum I get an integer but the issue is it is ALWAYS 220077.76354 every single time, not random at all. Also I do not believe the modulo function is available to VBScript.
Thanks,
BMD

4. ## Re: Randomize Odd or Even

In vbScript modulus is Mod

<font class="small">Code:</font><hr /><pre>
result = num1 Mod num2
</pre><hr />

5. ## Re: Randomize Odd or Even

Thanks Aaron,

The issue about the random number not being random remains. I'm able to get result to be a 1 or 0 but it is always that number as randomNum = (Rnd(1-2))*1000000 always returns the same number.
BMD

6. ## Re: Randomize Odd or Even

With VBScript you need to seed the random number first using the "Randomize" statement.

7. ## Re: Randomize Odd or Even

This is the code bit and it appears to be working.
<font class="small">Code:</font><hr /><pre>
Randomize
randomNum = (Rnd(1))*100
iNum = Int(randomNum)
result = iNum Mod 2
</pre><hr />
I had the randomize statement in the code.
Thanks, for all the pointers.
BMD

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 10.71%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 06:12 AM.

Copyright BetaSoft Inc.