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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:15:10+00:00 2026-06-14T09:15:10+00:00

My friend gave me a bit of code for my SDL program, all I

  • 0

My friend gave me a bit of code for my SDL program, all I know that it makes a random color but I have no idea how it works , here is the code

 int unsigned temp = 10101;//seed
    for(int y = 0;y < times;y++){
        temp = temp*(y+y+1);
        temp = (temp^(0xffffff))>>2;
        //printf("%x\n",temp);
        SDL_FillRect(sprite[y],NULL,temp);
        SDL_BlitSurface(sprite[y],&paste[y],rScreen(),NULL);
        }

My question is , How does this code work and how does it make a random color

  • 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-14T09:15:11+00:00Added an answer on June 14, 2026 at 9:15 am

    Your friend is creating a “random RGB value” ranging from 0x000000 to 0xFFFFFF out of some amateur PRNG he invented.

    I’ll explain the code with comments:

    This is the so called “seed”. The initial value that will generate the pseudo-random sequence of values.

     int unsigned temp = 10101; //seed 
    

    then we got the for loop:

     for(int y = 0;y < times;y++)
     {
        temp = temp*(y+y+1);
        temp = (temp^(0xffffff))>>2;
    

    each round your friend is trying to make complicated multiplications and sums to come up with a new temp value which is divided by 2 (the >>2 in the code above) and then masked with 0xFFFFFFF to get a value in the range of 0x000000 to 0xFFFFFFF (he wrongly used bitwise XOR ^ instead of bitwise AND &)

    The resulting value is used as a RGB value for the SDL_FillRect() function.

        //printf("%x\n",temp);
        SDL_FillRect(sprite[y],NULL,temp);
        SDL_BlitSurface(sprite[y],&paste[y],rScreen(),NULL);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A friend gave me a solution that uses this jquery code: .css({ backgroundColor: '#ddd'
A friend of mine wanted help learning to program, so he gave me all
I have a Django site running with PSQL but when my friend gave the
I have a Django site running with PSQL but when my friend gave the
My friend gave me a database file: record.mdf . I copied that .mdf file
A friend gave me this code snippet in Clojure (defn sum [coll acc] (if
Alright, my friend gave me this code for requesting headers and comparing them to
I have a problem with encoding. A friend gave me his php/mysql project to
Okay, don't laugh, but my friend gave me a fix to a jQuery UI
I have a game source code but I could not assemble it on nasm

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.