本文整理汇总了C++中random_rand函数的典型用法代码示例。如果您正苦于以下问题:C++ random_rand函数的具体用法?C++ random_rand怎么用?C++ random_rand使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了random_rand函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: recv
/*---------------------------------------------------------------------------*/
static void
recv(struct broadcast_conn *bc, const rimeaddr_t *from)
{
struct trickle_conn *c = (struct trickle_conn *)bc;
uint16_t seqno = packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID);
PRINTF("%d.%d: trickle recv seqno %d from %d.%d our %d data len %d channel %d\n",
rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
seqno,
from->u8[0], from->u8[1],
c->seqno,
packetbuf_datalen(),
packetbuf_attr(PACKETBUF_ATTR_CHANNEL));
if(seqno == c->seqno)
{
/* c->cb->recv(c);*/
++c->duplicates;
}
else if(SEQNO_LT(seqno, c->seqno))
{
c->interval_scaling = 0;
send(c);
}
else /* hdr->seqno > c->seqno */
{
#if CONTIKI_TARGET_NETSIM
/* ether_set_line(from->u8[0], from->u8[1]);*/
#endif /* CONTIKI_TARGET_NETSIM */
c->seqno = seqno;
/* Store the incoming data in the queuebuf */
if(c->q != NULL)
{
queuebuf_free(c->q);
}
c->q = queuebuf_new_from_packetbuf();
c->interval_scaling = 0;
reset_interval(c);
ctimer_set(&c->first_transmission_timer, random_rand() % c->interval,
send, c);
c->cb->recv(c);
}
}
示例2: handler_802154_frame_received
/*---------------------------------------------------------------------------*/
int
handler_802154_frame_received(frame802154_t *frame)
{
if(answer_beacon_requests &&
frame->fcf.frame_type == FRAME802154_CMDFRAME &&
frame->payload[0] == FRAME802154_BEACONREQ) {
others_beacon_reply = 0;
ctimer_set(NULL, &beacon_send_timer,
CLOCK_SECOND / 16 +
(CLOCK_SECOND * (random_rand() & 0xff)) / 0x200,
&handle_beacon_send_timer, NULL);
return 1;
}
if(frame->fcf.frame_type == FRAME802154_BEACONFRAME) {
HANDLER_802154_STAT(handler_802154_stats.beacons_received++);
handle_beacon(frame);
return 1;
}
return 0;
}
示例3: PROCESS_THREAD
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(multicast_example_process, ev, data)
{
PROCESS_BEGIN();
/* Create a linkl-local multicast addresses. */
uip_ip6addr(&addr, 0xff02, 0, 0, 0, 0, 0, 0x1337, 0x0001);
/* Join local group. */
if(uip_ds6_maddr_add(&addr) == NULL) {
printf("Error: could not join local multicast group.\n");
}
/* Register UDP socket callback */
udp_socket_register(&s, NULL, receiver);
/* Bind UDP socket to local port */
udp_socket_bind(&s, PORT);
/* Connect UDP socket to remote port */
udp_socket_connect(&s, NULL, PORT);
while(1) {
/* Set up two timers, one for keeping track of the send interval,
which is periodic, and one for setting up a randomized send time
within that interval. */
etimer_set(&periodic_timer, SEND_INTERVAL);
etimer_set(&send_timer, (random_rand() % SEND_INTERVAL));
PROCESS_WAIT_UNTIL(etimer_expired(&send_timer));
printf("Sending multicast\n");
udp_socket_sendto(&s,
"hello", 6,
&addr, PORT);
PROCESS_WAIT_UNTIL(etimer_expired(&periodic_timer));
}
PROCESS_END();
}
示例4: new_dio_interval
static void
new_dio_interval(rpl_dag_t *dag)
{
uint32_t time;
/* TODO: too small timer intervals for many cases */
time = 1UL << dag->dio_intcurrent;
/* Convert from milliseconds to CLOCK_TICKS. */
time = (time * CLOCK_SECOND) / 1000;
dag->dio_next_delay = time;
/* random number between I/2 and I */
time = time >> 1;
time += (time * random_rand()) / RANDOM_RAND_MAX;
/*
* The intervals must be equally long among the nodes for Trickle to
* operate efficiently. Therefore we need to calculate the delay between
* the randomized time and the start time of the next interval.
*/
dag->dio_next_delay -= time;
dag->dio_send = 1;
#if RPL_CONF_STATS
/* keep some stats */
dag->dio_totint++;
dag->dio_totrecv += dag->dio_counter;
ANNOTATE("#A rank=Xu.Xu(Xu),stats=Xd Xd Xd Xd,color=Xs");//, DAG_RANK(dag->rank, dag), (10 * (dag->rank % dag->min_hoprankinc)) / dag->min_hoprankinc, dag->version, dag->dio_totint, dag->dio_totsend, dag->dio_totrecv,dag->dio_intcurrent, dag->rank == ROOT_RANK(dag) ? "BLUE" : "ORANGE");
#endif /* RPL_CONF_STATS */
/* reset the redundancy counter */
dag->dio_counter = 0;
/* schedule the timer */
PRINTF("RPL: Scheduling DIO timer Xlu ticks in future (Interval)");
PRINTF_DEC(time);
ctimer_set(&dag->dio_timer, time, &handle_dio_timer, dag);
}
示例5: assemble_data
/**
* subscribe node will assemble the data according to the conditions
*/
void assemble_data(){
uint8_t value = random_rand()%10;
assemble_total+=value;
assemble_count++;
if(value>max){
max = value;
}
if(assemble_count>= local_node.assemble_count){
Runicast_msg runicast_msg;
runicast_msg.native_sen_type = local_node.sen_type;
runicast_msg.seqno = clct_data_seqno;
//runicast_msg.timestamp = CLOCK_SECOND;
if(local_node.condition == MAX){
runicast_msg.value = max;
}else if(local_node.condition == AVERAGE ){
runicast_msg.value = (assemble_total)/(assemble_count);
}
rimeaddr_copy(&runicast_msg.last_hop,&rimeaddr_node_addr);
rimeaddr_copy(&runicast_msg.source,&rimeaddr_node_addr);
//add_to_RU_list(runicast_msg);
if(!runicast_is_transmitting(&runicast)){
printf("[PUBLISH] publish data value->%u seqno %u\n",runicast_msg.value,runicast_msg.seqno);
//single_publish(&runicast,runicast_msg);
printf("[RU] timestamp %d seqno %u last hop address %u.%u\n", runicast_msg.timestamp,runicast_msg.seqno,runicast_msg.last_hop.u8[0],runicast_msg.last_hop.u8[1]);
packetbuf_copyfrom(&runicast_msg, sizeof(Runicast_msg));
runicast_send(&runicast,&find_best_route()->rt_entry.next_hop,MAX_RETRANSMISSIONS);
packetbuf_clear();
}
if(clct_data_seqno>254){
clct_data_seqno = 0;
}
clct_data_seqno++;
assemble_count = 0;
assemble_total = 0.0f;
max = 0.0f;
}
}
示例6: uip_ds6_init
/*---------------------------------------------------------------------------*/
void
uip_ds6_init(void)
{
uip_ds6_neighbors_init();
uip_ds6_route_init();
PRINTF("Init of IPv6 data structures\n");
PRINTF("%u neighbors\n%u default routers\n%u prefixes\n%u routes\n%u unicast addresses\n%u multicast addresses\n%u anycast addresses\n",
NBR_TABLE_MAX_NEIGHBORS, UIP_DS6_DEFRT_NB, UIP_DS6_PREFIX_NB, UIP_DS6_ROUTE_NB,
UIP_DS6_ADDR_NB, UIP_DS6_MADDR_NB, UIP_DS6_AADDR_NB);
memset(uip_ds6_prefix_list, 0, sizeof(uip_ds6_prefix_list));
memset(&uip_ds6_if, 0, sizeof(uip_ds6_if));
uip_ds6_addr_size = sizeof(struct uip_ds6_addr);
uip_ds6_netif_addr_list_offset = offsetof(struct uip_ds6_netif, addr_list);
/* Set interface parameters */
uip_ds6_if.link_mtu = UIP_LINK_MTU;
uip_ds6_if.cur_hop_limit = UIP_TTL;
uip_ds6_if.base_reachable_time = UIP_ND6_REACHABLE_TIME;
uip_ds6_if.reachable_time = uip_ds6_compute_reachable_time();
uip_ds6_if.retrans_timer = UIP_ND6_RETRANS_TIMER;
uip_ds6_if.maxdadns = UIP_ND6_DEF_MAXDADNS;
uip_ds6_set_lladdr(&uip_lladdr);
#if UIP_CONF_ROUTER
#if UIP_ND6_SEND_RA
stimer_set(&uip_ds6_timer_ra, 2); /* wait to have a link local IP address */
#endif /* UIP_ND6_SEND_RA */
#else /* UIP_CONF_ROUTER */
etimer_set(&uip_ds6_timer_rs,
random_rand() % (UIP_ND6_MAX_RTR_SOLICITATION_DELAY *
CLOCK_SECOND),
&tcpip_process);
#endif /* UIP_CONF_ROUTER */
etimer_set(&uip_ds6_timer_periodic, UIP_DS6_PERIOD, &tcpip_process);
return;
}
示例7: generate_random_pan_id_and_aes_key
void generate_random_pan_id_and_aes_key(void) {
/* PAN ID and AES key is only generated once at first startup */
if (eeprom_read_byte ((const void *)EE_FIRST_RUN_FLAG))
{
uint8_t key[16];
//AES key
rf212_generate_key(key);
eeprom_write_block(key, (void *)EE_ENCRYPTION_KEY, EE_ENCRYPTION_KEY_SIZE);
//PAN ID
uint16_t rand_pan_id = 0xFFFF;
//init random number generator with first 2 bytes of AES key
random_init(eeprom_read_word ((const void *)EE_ENCRYPTION_KEY));
//generate random pan_id which is not the broadcast_pan_id and not the bootloader/provisioning pan_id
while (rand_pan_id == 0xFFFF || rand_pan_id == 0x0001)
{
rand_pan_id = random_rand();
}
eeprom_write_word ((void *)EE_PAN_ID, rand_pan_id);
eeprom_write_byte ((void *)EE_FIRST_RUN_FLAG, 0x00);
}
}
示例8: on_interval_expired
/* Corresponds to Rule 6 of Trickle */
static void
on_interval_expired(void *ptr)
{
clock_time_t half_interval_size;
if(trickle_doublings < IMAX) {
trickle_doublings++;
PRINTF("akes-trickle: Doubling interval size\n");
}
half_interval_size = interval_size() / 2;
new_nbrs_count = 0;
counter = 0;
ctimer_set(&trickle_timer,
half_interval_size + ((half_interval_size * random_rand()) / RANDOM_RAND_MAX),
on_timeout,
NULL);
PRINTF("akes-trickle: I=%lus t=%lus\n",
interval_size()/CLOCK_SECOND,
trickle_timer.etimer.timer.interval/CLOCK_SECOND);
}
示例9: PROCESS_THREAD
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(collectd_process, ev, data)
{
static struct etimer period_timer, wait_timer;
PROCESS_BEGIN();
collectd_conf_init(&collectd_conf);
/* new connection with remote host */
client_conn = udp_new(NULL, UIP_HTONS(0), NULL);
udp_bind(client_conn, UIP_HTONS(COLLECTD_CLIENT_PORT));
PRINTF("Created a connection with the server ");
/* Send a packet every 60-62 seconds. */
etimer_set(&period_timer, CLOCK_SECOND * DEFAULT_UPDATE_PERIOD);
while(1) {
PROCESS_WAIT_EVENT();
//send update (collected data)
if(ev == PROCESS_EVENT_TIMER) {
if (data == &period_timer) {
etimer_reset(&period_timer); //TODO: reset the period from collectd_conf.update freq
if (collectd_conf.send_active == SEND_ACTIVE_YES)
etimer_set(&wait_timer, random_rand() % (CLOCK_SECOND * RANDWAIT));
} else if(data == &wait_timer) {
/* Time to send the data */
PRINTF("Time to send the data\n");
collectd_common_send(client_conn, &collectd_conf);
}
}
//receive a request (here, expected a request to begin to send update)
if(ev == tcpip_event) {
collectd_udp_handler();
}
}
PROCESS_END();
}
示例10: receiver
/*---------------------------------------------------------------------------*/
static void
receiver(struct simple_udp_connection *c,
const uip_ipaddr_t *sender_addr,
uint16_t sender_port,
const uip_ipaddr_t *receiver_addr,
uint16_t receiver_port,
const uint8_t *data,
uint16_t datalen)
{
printf("Data (%d) received on port %d from port %d with length %d\n",
*data, receiver_port, sender_port, datalen);
#if RANDOM_TOKEN_ERROR
if (0 == random_rand() % RANDOM_ERROR) {
// this is written just to introduce random error to the algorithm
printf("RANDOM Error Occurred\n");
token = 1;
}
#endif
if ( (node_id - ((*data) % NUM_MOTES)) == 1)
token = 1;
}
示例11: uip_ds6_send_ra_periodic
/*---------------------------------------------------------------------------*/
void
uip_ds6_send_ra_periodic(void)
{
if(racount > 0) {
/* send previously scheduled RA */
uip_nd6_ra_output(NULL);
PRINTF("Sending periodic RA\n");
}
rand_time = UIP_ND6_MIN_RA_INTERVAL + random_rand() %
(uint16_t) (UIP_ND6_MAX_RA_INTERVAL - UIP_ND6_MIN_RA_INTERVAL);
PRINTF("Random time 1 = %u\n", rand_time);
if(racount < UIP_ND6_MAX_INITIAL_RAS) {
if(rand_time > UIP_ND6_MAX_INITIAL_RA_INTERVAL) {
rand_time = UIP_ND6_MAX_INITIAL_RA_INTERVAL;
PRINTF("Random time 2 = %u\n", rand_time);
}
racount++;
}
PRINTF("Random time 3 = %u\n", rand_time);
stimer_set(&uip_ds6_timer_ra, rand_time);
}
示例12: servreg_hack_register
/*---------------------------------------------------------------------------*/
void
servreg_hack_register(servreg_hack_id_t id, const uip_ipaddr_t *addr)
{
static servreg_hack_item_t *t;
static struct servreg_hack_registration *r;
/* Walk through list, see if we already have a service ID
registered. If not, allocate a new registration and put it on our
list. If we cannot allocate a service registration, we reuse one
from the service registrations made by others. */
servreg_hack_init();
for(t = list_head(own_services);
t != NULL;
t = list_item_next(t)) {
if(servreg_hack_item_id(t) == id) {
return;
}
}
r = memb_alloc(®istrations);
if(r == NULL) {
printf("servreg_hack_register: error, could not allocate memory, should reclaim another registration but this has not been implemented yet.\n");
return;
}
r->id = id;
r->seqno = 1;
uip_ipaddr_copy(&r->addr, addr);
timer_set(&r->timer, LIFETIME / 2);
list_push(own_services, r);
PROCESS_CONTEXT_BEGIN(&servreg_hack_process);
etimer_set(&sendtimer, random_rand() % (NEW_REG_TIME));
PROCESS_CONTEXT_END(&servreg_hack_process);
}
示例13: run_trickle
/*---------------------------------------------------------------------------*/
static int
run_trickle(struct trickle_conn *c)
{
clock_time_t interval;
PT_BEGIN(&c->pt);
while(1) {
interval = c->interval << c->interval_scaling;
set_timer(c, &c->interval_timer, interval);
set_timer(c, &c->t, interval / 2 + (random_rand() % (interval / 2)));
c->duplicates = 0;
PT_YIELD(&c->pt); /* Wait until listen timeout */
if(c->duplicates < DUPLICATE_THRESHOLD) {
send(c);
}
PT_YIELD(&c->pt); /* Wait until interval timer expired. */
if(c->interval_scaling < INTERVAL_MAX) {
c->interval_scaling++;
}
}
PT_END(&c->pt);
}
示例14: ipolite_send
/*---------------------------------------------------------------------------*/
int
ipolite_send(struct ipolite_conn *c, clock_time_t interval, uint8_t hdrsize)
{
if(c->q != NULL) {
/* If we are already about to send a packet, we cancel the old one. */
PRINTF("%d.%d: ipolite_send: cancel old send\n",
linkaddr_node_addr.u8[0],linkaddr_node_addr.u8[1]);
queuebuf_free(c->q);
c->q = NULL;
ctimer_stop(&c->t);
}
c->dups = 0;
c->hdrsize = hdrsize;
if(interval == 0) {
PRINTF("%d.%d: ipolite_send: interval 0\n",
linkaddr_node_addr.u8[0],linkaddr_node_addr.u8[1]);
if(broadcast_send(&c->c)) {
if(c->cb->sent) {
c->cb->sent(c);
}
return 1;
}
} else {
c->q = queuebuf_new_from_packetbuf();
if(c->q != NULL) {
ctimer_set(&c->t,
interval / 2 + (random_rand() % (interval / 2)),
send, c);
return 1;
}
PRINTF("%d.%d: ipolite_send: could not allocate queue buffer\n",
linkaddr_node_addr.u8[0],linkaddr_node_addr.u8[1]);
}
return 0;
}
示例15: storage_generate_file
char *
storage_generate_file(char *prefix, unsigned long size)
{
static char filename[ATTRIBUTE_NAME_LENGTH + sizeof(".ffff")];
#if !DB_FEATURE_COFFEE
int fd;
#endif
snprintf(filename, sizeof(filename), "%s.%x", prefix,
(unsigned)(random_rand() & 0xffff));
#if DB_FEATURE_COFFEE
PRINTF("DB: Reserving %lu bytes in %s\n", size, filename);
if(cfs_coffee_reserve(filename, size) < 0) {
PRINTF("DB: Failed to reserve\n");
return NULL;
}
return filename;
#else
fd = cfs_open(filename, CFS_WRITE);
cfs_close(fd);
return fd < 0 ? NULL : filename;
#endif /* DB_FEATURE_COFFEE */
}