Posts
Showing posts with the label Random
In Python, list of random integers
- Get link
- X
- Other Apps
def change_list(src_list): # hold the middle 6 elements of the list dest_list = src_list[3:9] # use a slice to make a new list # Print the size print('The size of the list is now', len(dest_list)) # sort the new list in ascending order dest_list.sort() return dest_list def main(): # create an empty list my_list = [] from random import randrange # use a for loop to add 12 random integers for x in range(12): # all ranging from 50 to 100 my_list.append(randrange(50, 100)) # for loop to iterate over the list and display # all elements on one line separated by a single space. print('Here is the list of random integers...', end=' ') for x in my_list: print(x, end=' ') print() # display the 4th element print('The 4th element in the list is', my_list[3]) # the element at index 9 print('The element at index 9 is', my_list[9]) # the smallest element print('The smallest eleme...
In Python, Scramble String And Check If Two Strings Are Anagram
- Get link
- X
- Other Apps
In Java, Write And Read 100 Random Integers From File
- Get link
- X
- Other Apps
import java.io.PrintStream; public class Ex3{ public static void main(String[]args){ try(PrintStream out = new PrintStream(new java.io.File("numbers.txt"))){ int i = 100; while(0 < i --) out.println((int)(1+Math.random()*10000)); }catch(java.io.IOException e){ System.err.println(e.getMessage()); } } } Download
In Java, Generate Random Number Excluding Given Array Of Integers
- Get link
- X
- Other Apps
class RandNum { public static void main(String[] args) { System.out.println("Random: " + getRandom(33, 43, 47, 7, 11, 54, 21)); } public static int getRandom(int... numbers){ int rand, len; do{ rand = (int)(1+Math.random()*54); len = numbers.length; while(0 < len && rand != numbers[--len]); if(0 == len && rand != numbers[0]) return rand; }while(true); } } Download
Guess 3 lucky numbers and win credits with C++/Cpp programming
- Get link
- X
- Other Apps
#include<iostream> #include<cstdlib> using namespace std; int main(){ int lucky[3]; srand(time(0)); const int max = sizeof(lucky)/sizeof(int); for(int i = max-1; 0 <= i; i --){ lucky[i] = (int)(rand() % 10); } int guess[max]; cout << "Guess " << max << " numbers: "; for(int i = max-1; 0 <= i; i --){ cin >> guess[i]; } int credits = 0; cout << endl << "Your guess: "; for(int i = max-1; 0 <= i; i --){ cout << guess[i]; if(i) cout << ", "; if(guess[i] == lucky[i]) credits ++; } cout << endl << "Lucky numbers: "; for(int i = max-1; 0 <= i; i --){ cout << lucky[i]; if(i) cout << ", "; } cout << endl << "You won credits: " << credits << endl; return 0; } Download
Append 100 random integers to binary file with C++/Cpp programming
- Get link
- X
- Other Apps
#include<iostream> #include<fstream> #include<cstdlib> #include<ctime> using namespace std; int main(){ ofstream out("Exercise13_13.dat", (ios::out | ios::binary | ios_base::app)); if(!out){ cerr << "Error: failed to open file!" << endl; return 1; } srand(time(0)); int num; int bytes = sizeof(int); const char *addr = reinterpret_cast<const char*>(&num); for(int i = 1; 100 >= i; i++){ num = rand() % 100; out.write(addr, bytes); } out.close(); return 0; } Download
Fill 100 random integers in array and count even and odds in array with Java
- Get link
- X
- Other Apps
public class ArrayTest{ public static int[] createArray(){ int[]arr = new int[100]; for(int i = arr.length-1; 0 <= i; i --){ arr[i] = (int)(Math.random()*100); } return arr; } public static void statsDisplay(int[] intArray){ int evens = 0, odds = 0, sum = 0; for(int n: intArray){ sum += n; if(0 == n%2){ evens ++; } else { odds ++; } } double avg = sum/intArray.length*1.0; System.out.printf("evens: %d, odds: %d, avg: %.2f%n", evens, odds, avg); } public static void main(String[]args){ statsDisplay(createArray()); } } Download
How to generate random colors using Java Program?
- Get link
- X
- Other Apps
import java.awt.Color; public class Randomize{ public static void main(String[]args){ System.out.println(randomColor()); System.out.println(randomColor()); } public static Color randomColor(){ return new Color( randomInRange(0,255), randomInRange(0,255), randomInRange(0,255) ); } public static int randomInRange(int a, int b){ return (int)(a + (Math.random()*(b-a))); } } Download