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 7904545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:07:34+00:00 2026-06-03T10:07:34+00:00

I want to choose randomly from among 4 directions: movePlayer(map, &positionPlayer, direction); where direction

  • 0

I want to choose randomly from among 4 directions:

movePlayer(map, &positionPlayer, direction);

where direction is one of UP, DOWN, LEFT, and RIGHT.

I haven’t yet found out how to do this with the rand() function.

How can I do this? Do I need to assign a number to each direction and then choose a random number within those bounds?

Thank you for your help.

  • 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-06-03T10:07:35+00:00Added an answer on June 3, 2026 at 10:07 am

    The simplest way to do this is to simply do a modulos of the rand function to get a numbers between 0 and 3(inclusive).

    int x = rand() % 4;
    

    Then you can simply perform a switch statement to choose the direction

    switch(x)
    {
       case 0:
          movePlayer(map, &positionPlayer, UP);
          break;
    
       case 1:
          movePlayer(map, &positionPlayer, DOWN);
          break;
    
       case 2:
          movePlayer(map, &positionPlayer, LEFT);
          break;
    
       case 3:
          movePlayer(map, &positionPlayer, RIGHT);
          break;
    }
    

    Of course, this will not be exactly random with a perfect distrubition( the modulus is somewhat of a hack).

    If you want real randomness with actual distributions then you might want to look into Boost.Random.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In views.py I want to randomly choose one record with my filter: a=Entry.objects.filter(first_name__contains='Br')).order_by('?')[0] b=a.id
I want to randomly choose a value from a small range of integers (less
I want to choose an entry from my database randomly, but not on every
I've learned several languages, but now I want to choose one. The language that
I want the user to be able to choose from a dropdown Combobox listing
I want to post all text box responses to results.php and then randomly choose
Can I use OpenFileDialog in Android? I want to choose image from OpenFileDialog and
I want a javascript script that choose either value1 or value2 randomly, not between
I want to choose a row from a certain table and order the results
I want to choose randomly* between two alternatives with unequal probability. For instance, when

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.