int rand7() { return rand()%7+1; } int rand10(){ int x=0; do { x=(rand7()-1)*7+rand7(); } while(x>40); return x%10+1;}
Analysis: To ensure the even distribution of rand10 () on integers 1-10, you can construct a random integer interval with a
From: http://blog.csdn.net/furturerock/article/details/6850634View Code
1 int rand7() 2 { 3 return rand()%7+1; 4 } 5 6 int rand10() 7 { 8 int x=0; 9 do10 {11 x=(rand7()-1)*7+rand7();12 }13 while(x>40);14 return x%10+1;15 }
Analysis: To ensure the even distribution of rand10 () on integers
cookie| Program | Random
/*-----------------------------------------------------
At the request of a few colleagues, wrote the program for them, and I never buy lottery tickets, do not understand.
But if you're really in the jackpot, don't forget to tell me. What are you afraid of me splitting your money?
I am not greedy, the most send me a classic music dish I am very happy!
By Jianglixin@163.net (hastily written, code messy collation) 2001.2.28
---
1. Int rand7 ()2 .{3. Return rand () % 7 + 1;4 .}5.6. Int rand10 ()7 .{8. Int x = 0;9. Do10 .{11. x = (rand7 ()-1) * 7 + rand7 ();12 .}13. While (x> 40 );14. Return X % 10 + 1;15 .}Analysis: To ensure the even distribution of rand10 () on integers 1-10, you can construct a random integer interval with a
It's actually very easy to suddenly find the problem today
X = rand (1, N) to generate [0, 1] Even random books...
Y = x/sum (x )...
Today, I encountered a problem when I wrote a random simulation program;X1, x2,..., xn random number
) occurs means that if a function Would generate all the numbers between 0 and MOD-1, it'll generate pseudo-random numbers uniformly with every MOD Iterati Ons.If STEP = + MOD =, the function generates the series 0, 5 (or any other repeating series if the initial SE Ed is other than 0). This is a poor selection of STEP and MOD because no initial seed would generate all of the numbers from 0 and MOD-
true sense.
In order for the program to generate random values for a new sequence each time it executes, we usually provide a new random seed for the random number generator. The function Srand () (from stdlib.h) can spread the seed for the
One, random number and timeRandom number generator (JS:math.random)echo rand ();Generate a random number in a rangeecho rand (0,10);Time stampecho Time ();Format the display of timeecho "echo Date ("Y-m-d h:i:s", Time ());echo "ec
1. Use function Np.random.random
Because Np.random.random () defaults to generating decimals between 0~1, you need to convert
such as the random number matrix between the -1~1 of generating 3*3
-
1. The static (static) method in the Math Library random ()The function of this method is to produce a double value between 0 and 1 (including 0, but not 1).double rand = Math.random ();2, through the Random class objectA program can generate many different types of
the parameter is Ganso:In [41]: random.choice((1,3,7,4))Out[41]: 1In [42]: random.choice((1,3,7,4))Out[42]: 7
Random.shuffle: If you want to shuffle an element in a sequence (excluding Ganso and strings), you can use this function method
In [49]: list = [1,2,3,4]In [50]: random.shuffle(list)In [51]: listOut[51]: [4, 2, 1, 3]
Random.sample (A, B): A fragment of the specified length B
Original article: http://www.cnblogs.com/nokiaguy/archive/2008/05/11/1191906.html
10000000 non-repeated random numbers must be generated between 99999999 and 1 million. You can create a table named Table1, which has two fields: the random number field generated (INT type) and the int type field.
Use 99999999-1000
Question 21: programming, input two integers n and m, from Series 1, 2, 3 ,...... N is random, so that the sum is equal to m. All possible combinations are required. It is actually a backpack problem. Solution: 1. First, determine that if nm is used, the number of n greater than m cannot be involved in the combination,
Package Test;import Java.util.arraylist;import java.util.arrays;import java.util.list;import java.util.Random; public class Createarraylist {public static void main (string[] args) {listThe program creates a ArrayList, adds 10 random numbers from 1 to 10, deletes a number greater than 5 Java
1 Public int[] Getnorepeatarrayy (intarrlength)2 {3 int[] Array =New int[arrlength];4ilistint> list =Newlistint>();5 //prepare data that is not duplicated6 for(inti =0; I )7 {8 list. ADD (i);9 }Ten //randomly inserting non-repeating data into an array One for(intj = (list. Count-1); J >-
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.