Raised This Month: $51 Target: $400
 12% 

Solved Random func.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 06-11-2017 , 10:16   Random func.
Reply With Quote #1

How to deal with random_num func, making the last digit of random number equal to 0.
__________________

Last edited by Relaxing; 06-12-2017 at 15:27.
Relaxing is offline
DjSoftero
Veteran Member
Join Date: Nov 2014
Location: Lithuania
Old 06-11-2017 , 11:36   Re: Random func.
Reply With Quote #2

this should be posted in a scripting help.

PHP Code:
new number random_num(1236)
//if(number%10 != 0) you don`t even need if statement, because while() will do this check anyways
    
while(number%10 != 0)
        
number-- 
as of right now i came up only with this, but i`m sure there is an easier way.
also to be noted: This will work only with numbers up to 99
__________________
retired chump

Last edited by DjSoftero; 06-11-2017 at 11:37. Reason: optimization
DjSoftero is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 06-11-2017 , 13:25   Re: Random func.
Reply With Quote #3

Quote:
Originally Posted by DjSoftero View Post
this should be posted in a scripting help.

PHP Code:
new number random_num(1236)
//if(number%10 != 0) you don`t even need if statement, because while() will do this check anyways
    
while(number%10 != 0)
        
number-- 
as of right now i came up only with this, but i`m sure there is an easier way.
also to be noted: This will work only with numbers up to 99

I dont know how you guys understand each other like some sort of magic, anyway your question dosent make sense to me at all. Maybe you need to explain more on what you doing
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
DjSoftero
Veteran Member
Join Date: Nov 2014
Location: Lithuania
Old 06-11-2017 , 14:16   Re: Random func.
Reply With Quote #4

Quote:
Originally Posted by Natsheh View Post
I dont know how you guys understand each other like some sort of magic, anyway your question dosent make sense to me at all. Maybe you need to explain more on what you doing
lol

I understood it like this: if a number is: 19, it becomes 10, if 99, then 90 and etc
__________________
retired chump
DjSoftero is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-11-2017 , 15:11   Re: Random func.
Reply With Quote #5

If you need the last digit to be a zero, simply multiply by 10, much more efficient and deterministic.
__________________

Last edited by fysiks; 06-11-2017 at 15:12.
fysiks is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 06-11-2017 , 18:34   Re: Random func.
Reply With Quote #6

Quote:
Originally Posted by fysiks View Post
If you need the last digit to be a zero, simply multiply by 10, much more efficient and deterministic.
He wants the last digit to be zero not add another zero.

Perhaps this is what you're looking for.
Code:
#include < amxmodx > public plugin_init( ) {     new iNum = random_num( 11, 19 ), szString[ 2 ], iNumConverted ;     num_to_str( iNum, szString, charsmax( szString ) ) ;     replace( szString, charsmax( szString ), szString[ sizeof szString - 1 ], "0" ) ;         iNumConverted = str_to_num( szString ) ;         console_print( 0, "Old Number : %d | Converted Number : %d", iNum, iNumConverted ) ; }
__________________

Last edited by edon1337; 06-11-2017 at 18:46.
edon1337 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-11-2017 , 20:36   Re: Random func.
Reply With Quote #7

Quote:
Originally Posted by edon1337 View Post
He wants the last digit to be zero not add another zero.
It's actually the same thing but implemented simpler. You simply need to properly adjust the range used with random_num().
__________________
fysiks is offline
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 06-12-2017 , 11:55   Re: Random func.
Reply With Quote #8

What if I use to choose between (0, 5000)? Will it work by checking the number's digits and setting the last one to zero?
__________________
Relaxing is offline
DjSoftero
Veteran Member
Join Date: Nov 2014
Location: Lithuania
Old 06-12-2017 , 12:31   Re: Random func.
Reply With Quote #9

Quote:
Originally Posted by Relaxing View Post
What if I use to choose between (0, 5000)? Will it work by checking the number's digits and setting the last one to zero?
converting the number into a string, then changing the last digit to the 0 is the easiest way. dunno about efficiency of that
__________________
retired chump
DjSoftero is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 06-12-2017 , 13:38   Re: Random func.
Reply With Quote #10

Quote:
Originally Posted by Relaxing View Post
What if I use to choose between (0, 5000)? Will it work by checking the number's digits and setting the last one to zero?
The method I provided above works only with numbers above 10. Here's a way for numbers below and above 10.
Code:
#include < amxmodx > public plugin_init( ) {     new iNum = random_num( 0, 5000 ), szString[ 2 ], iNumConverted ;     num_to_str( iNum, szString, charsmax( szString ) ) ;         if( ! szString[ 1 ] )     add( szString, charsmax( szString ), "0" ) ;         else     replace( szString, charsmax( szString ), szString[ sizeof szString - 1 ], "0" ) ;         iNumConverted = str_to_num( szString ) ;         console_print( 0, "Old Number : %d | Converted Number : %d", iNum, iNumConverted ) ; }
__________________

Last edited by edon1337; 06-12-2017 at 13:40.
edon1337 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:09.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode