XhCode Online Converter Tools

Random bitmap generator

Colors
Sizing
Shading
Random bitmap generator

A Random Bitmap Generator is a tool or function that generates a random image, typically represented as a bitmap (or a grid of pixels) where each pixel has a random color (often black and white, or full-color depending on your need).

Here's how you can generate random bitmaps in different programming languages and environments:

1. Random Bitmap Generator in Python
Python has libraries such as PIL (Python Imaging Library) or Pillow (a fork of PIL) that make it easy to generate random bitmaps.

Example: Generate a Random Black and White Bitmap
python

from PIL import Image
import random

# Define the image size
width, height = 100, 100

# Create a new image with '1' mode (black and white)
image = Image.new('1', (width, height))

# Generate random pixels
pixels = image.load()

for i in range(width):
for j in range(height):
# Randomly assign a black or white pixel
pixels[i, j] = random.choice([0, 255]) # 0 for black, 255 for white

# Save or show the image
image.show()
image.save('random_bitmap.png')
Explanation:
Image.new('1', (width, height)) creates a black-and-white image (mode '1').
The random.choice([0, 255]) selects between black (0) and white (255) for each pixel.
image.show() displays the image, and image.save() saves it as a PNG file.
2. Random Bitmap Generator in JavaScript (Canvas)
In JavaScript, you can use the Canvas API to draw a random bitmap on an HTML canvas.

Example: Generate a Random Black and White Bitmap on Canvas
html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Bitmap Generator</title>
</head>
<body>
<canvas id="bitmapCanvas" width="100" height="100"></canvas>
<script>
function generateRandomBitmap() {
const canvas = document.getElementById("bitmapCanvas");
const ctx = canvas.getContext("2d");

const width = canvas.width;
const height = canvas.height;

for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
// Randomly choose black or white
const color = Math.random() > 0.5 ? 'black' : 'white';
ctx.fillStyle = color;
ctx.fillRect(x, y, 1, 1); // Draw each pixel
}
}
}

generateRandomBitmap(); // Generate and display the bitmap
</script>
</body>
</html>
Explanation:
This HTML document uses a <canvas> element to draw the bitmap.
ctx.fillRect(x, y, 1, 1) draws a 1x1 pixel at each (x, y) coordinate.
The color for each pixel is randomly chosen between 'black' or 'white'.
3. Random Bitmap Generator in C (Using PPM Format)
If you want to generate a bitmap in C, you can create an image in PPM (Portable Pixmap) format, which is simple to handle as plain text.

Example: Generate a Random Black and White Bitmap in PPM Format
c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
// Define image dimensions
int width = 100;
int height = 100;

// Seed random number generator
srand(time(NULL));

// Open the output file
FILE *file = fopen("random_bitmap.ppm", "w");
if (!file) {
perror("Unable to open file");
return 1;
}

// Write PPM header
fprintf(file, "P3\n");
fprintf(file, "%d %d\n", width, height);
fprintf(file, "255\n"); // Max color value

// Generate random pixels (0 for black, 255 for white)
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int color = rand() % 2 == 0 ? 0 : 255; // Random 0 or 255
fprintf(file, "%d %d %d ", color, color, color); // RGB for black or white
}
fprintf(file, "\n");
}

// Close the file
fclose(file);

printf("Random bitmap saved as random_bitmap.ppm\n");
return 0;
}
Explanation:
The PPM format starts with a header P3, followed by image dimensions and color depth (255 for max intensity).
For each pixel, the RGB values are either 0 (black) or 255 (white) to create a black-and-white image.
The image is saved as random_bitmap.ppm, which can be opened in many image viewers.
4. Random Bitmap Generator in Excel
While Excel doesn't support direct bitmap generation, you can simulate a bitmap-like effect by generating random black-and-white cells.

Example: Generate Random Black and White Cells
Step 1: Set up the grid

Select a grid of cells (e.g., 100x100 cells).
Set the cell background color to white or black based on a random value.
Step 2: Use a formula to randomly color cells

In the first cell (e.g., A1), enter the following formula:
excel

=IF(RAND() < 0.5, "Black", "White")
Then, apply conditional formatting to color the cells black or white based on the text ("Black" or "White").
This won't create an actual bitmap image but will visually simulate a random bitmap within the Excel grid.