我必须编写一个c ++程序,但只有以下部分存在问题:
[...]为此目的,您将需要两个随机整数 集合{0,1,...,9}上的均匀分布(这两个数字必须是 以相同的概率生成)[...]
我不知道数学和c ++一样好,我也不允许使用c ++ 11。我写了以下简单的代码,但我怀疑它是否正确? :P
package javaapplication1;
import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.table.*;
public class TableD extends javax.swing.JFrame {
public TableD() {
initComponents();
Vector columnNames = new Vector();
Vector data = new Vector();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connect =DriverManager.getConnection("jdbc:odbc:cb1");
String sql = "Select * from tblbooks";
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery( sql );
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
stmt.close();
}
catch(Exception e) {
System.out.println( e );
}
JTable table = new JTable(data, columnNames);
//JScrollPane scrollPane = new JScrollPane( table );
//getContentPane().add( scrollPane );
//JPanel buttonPanel = new JPanel();
//getContentPane().add( buttonPanel, BorderLayout.SOUTH );
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(15, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(14, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TableD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TableD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TableD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TableD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
TableD frame = new TableD();
/*frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setTitle("Information Table");
frame.setVisible(true);*/
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TableD().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
答案 0 :(得分:1)
每个程序只调用一次 x y z
1: Pre A Lorem ipsum dolor sit amet, ac
2: Pre C Maecenas netus in, lacus fames
3: Pre C Conubia sed nulla dolor dui ap
4: Pre A Eget laoreet eros in aliquet e
5: Pre A Vestibulum quisque himenaeos p
6: Post A Sed venenatis. Ante turpis ege
7: Post A Ultricies ultricies eleifend m
8: Post B Conubia in in sed etiam nec mo
9: Post A Quam nec ex sodales dictum orc
10: Post A Sed ante id pulvinar maximus s
,而不是每次都需要一个随机数。对随机数使用模数技巧通常不会给出均匀分布。所以不,解决方案可能不正确。
答案 1 :(得分:0)
这取决于你是否真的需要它,或者只是非常接近。你的解决方案很接近,但并不完美。
假设您使用的是Microsoft的C ++,其中有RAND_MAX
of only 32767。获得0到7的几率为3277/32768,而获得8或9的几率仅为3276/32768。
StackOverflow上有很多关于如何获得完美分发的问题,例如:What is the optimal algorithm for generating an unbiased random integer within a range?