Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7661223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:34:10+00:00 2026-05-31T13:34:10+00:00

i am trying to make a very simple game where i have 7 positions

  • 0

i am trying to make a very simple game where i have 7 positions which are all hidden and within those there a 3 winning positions. I can pick randomly 3 times. I need to display whether the pick is a winning or not after every pick and store the result in a base.

Currently my thought where to generate an array of winning numbers on the first pick and then pick random number and check if it is in the winning array.

But i have a feeling that there is much more efficient way to do so.

Would appreciate if you would use PHP for coding examples, but pseudo code will do as-well.

EDIT
i am looking for the way to solve this without populating array with winning positions. maybe there is a way to do this with weights or probability percents.

Something like on first pick i have 3/7*100 percent chance to win. save result to base.
on second pick i have either 3/6*100 or 2/6*100 percent chance to win based weather i won in previous pick which i get from base.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:34:11+00:00Added an answer on May 31, 2026 at 1:34 pm

    Revised answer: this example does not require you to store the complete state of the game in a variable; instead, you just need to store the try count and won count:

    $won = 0;
    for($try = 0; $try < 3; $try++) {
        $slots = array_fill(0, 7 - $try, 0);  // blank slots
        $lucky = array_fill(0, 3 - $won, 1);  // lucky slots
        $pot = array_replace($slots, $lucky); // make some of the slots lucky
        $win = $pot[array_rand($pot)];        // randomly pick a slot
        $won += $win == 1;                    // update won count
        echo sprintf("Try %d: win=%d, total wins=%d\n", $try + 1, $win, $won);
    }
    

    Original answer:

    $pot = array(               // pot is (an associative) array; 0 = blank, 1 = win
        "pos_1" => 0,
        "pos_2" => 0,
        "pos_3" => 0,
        "pos_4" => 0,
        "pos_5" => 0,
        "pos_6" => 0,
        "pos_7" => 0
    );
    $win = array_rand($pot, 3); // picks three indexes from the pot randomly
    foreach($win as $w) {
        $pot[$w] = 1;           // set winning indicator
    }
    print_r($pot);
    

    Output: array containing state of the pots.

    Array
    (
        [pos_1] => 0
        [pos_2] => 1
        [pos_3] => 0
        [pos_4] => 1
        [pos_5] => 1
        [pos_6] => 0
        [pos_7] => 0
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a very simple game. In it I have objects (
I am trying to make a very simple web-service which does the following: The
i'm trying to make a very simple YACC parser on Pascal language which just
I'm trying to make a very simple OSGi test but I can't get it
I have a simple question, I'm trying to make an huge game for Windows
I Am trying to make a very simple irc bot in python, but can't
I am trying to make a very simple MFC OpenGL tutorial. There is a
I am trying to make a very simple application where the user can draw
I'm trying to make a very simple updater app that reads current version.txt file
I am trying to make a very simple object rotate around a fixed point

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.