Mysidia Adoptables Support Forum  

Home Community Mys-Script Creative Off-Topic
Go Back   Mysidia Adoptables Support Forum > Mysidia Adoptables > Questions and Supports

Notices

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 01-30-2010, 01:37 AM
Quillink Quillink is offline
Member
 
Join Date: Dec 2008
Posts: 93
Credits: 11,294
Quillink
Lightbulb PHP: Have a 1/2 chance to add or subtract 1 ..?

I'm trying to create a code that will take an existing number (from a table) and have a one-in-two chance of adding or subtracting one from it.
This is what I have at the moment:

Code:
// $numnew = new number
// $curnum = current number
// $max = upper limit (5 in this case)

if (rand(1,2) == 1) {
$numnew = ($curnum + rand(-1,1));
}

if ($numnew == 0) {
$numnew = $max;
}

if ($numnew > $max) {
$numnew = 1;
}
I've been trying to figure how to do this all day. This code works... kind of.
It tends to favor staying at either 1 or 5. I heard that the rand() function isn't truly random, so I guess this is what they meant. Is there any workaround?
Also, the number will occasionally and unexplainably jump to 5 no matter what $curnum was. :(

If anyone can help make this work (or better, show me how to write it better from scratch) I'd very much appreciate it. :D


EDIT: Should I be posting these random php questions in the Webmaster Bat Cave, Brandon? I just figured since they apply to my adoptable site they belonged here. :S
  #2  
Old 01-30-2010, 03:12 PM
BMR777 BMR777 is offline
Member
 
Join Date: Jan 2011
Posts: 1,122
Gender: Male
Credits: 10,723
BMR777 is on a distinguished road
Default RE: PHP: Have a 1/2 chance to add or subtract 1 ..?

The rand() function isn't truly random. I don't know why it isn't working, but I'll think about it and let you know if I come up with any ideas.
  #3  
Old 04-13-2011, 12:07 AM
Plague Plague is offline
Member
 
Join Date: Nov 2010
Posts: 50
Gender: Unknown/Other
Credits: 3,103
Plague is an unknown quantity at this point
Default

I could be wrong but I think the issue is here:

Code:
// $numnew = new number
// $curnum = current number
// $max = upper limit (5 in this case)

if (rand(1,2) == 1) {
$numnew = ($curnum + rand(-1,1));
}

if ($numnew == 0) {
$numnew = $max;
}

if ($numnew > $max) {
$numnew = 1;
}
I think maybe setting it like this:

Code:
if (rand(0,4) == 1) {
That might rectify the issue.
  #4  
Old 04-13-2011, 07:49 AM
AlexC's Avatar
AlexC AlexC is offline
Moderator
 
Join Date: Dec 2009
Location: Canada
Posts: 753
Gender: Unknown/Other
Credits: 67,341
AlexC is an unknown quantity at this point
Default

this thread is over a year old, the post date is January 2010. I believe (like most forums) that you aren't suppose to bump old threads.
  #5  
Old 04-13-2011, 09:08 AM
fadillzzz fadillzzz is offline
Dev Staff
 
Join Date: Jan 2010
Posts: 501
Gender: Male
Credits: 32,882
fadillzzz is an unknown quantity at this point
Default

Quote:
Originally Posted by Gloometh View Post
this thread is over a year old, the post date is January 2010. I believe (like most forums) that you aren't suppose to bump old threads.
You took the words right out of my mouth

Closed!
Closed Thread


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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Any chance.. lady jive Questions and Supports 3 03-29-2009 02:03 PM


All times are GMT -5. The time now is 01:42 PM.

Currently Active Users: 9727 (0 members and 9727 guests)
Threads: 4,080, Posts: 32,024, Members: 2,016
Welcome to our newest members, jolob.
BETA





What's New?

What's Hot?

What's Popular?


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
vBCommerce I v2.0.0 Gold ©2010, PixelFX Studios
vBCredits I v2.0.0 Gold ©2010, PixelFX Studios
Emoticons by darkmoon3636