I need to randomize fixed numbers, for example, 1,3,5,7,10. My output will be only 1,3,5,7 or 10. Please Help me! Thanks! Is there a way to randomize fixed numbers or user inputted numbers?
Get a random number from 0-4, then select from a dictionary of { 1, 3, 5, 7, 10 }.
int[] select = new int[] { 1, 3, 5, 7, 10 };
var rand = new Random();
int num = select[rand.Next(5)];
Related
import 'dart:io';
void main() {
int? n = int.parse(stdin.readLineSync()!);
print("value of n: $n and run time type: ${n.runtimeType}");
List<int> list1 = [];
for (int i = 0; i < n; i++) {
list1.insert(i, int.parse(stdin.readLineSync()!));
}
print("List 1: $list1");
}
This is my code.
If I take in put like that:
5
1
2
3
4
5
it gives output:
value of n: 5 and run time type: int
List 1: [1, 2, 3, 4, 5]
I want to take input like:
5
1 2 3 4 5
and want the same output. But I do not find any resources for that. Please help me.
You can split the string by using space as delimiter and then use map to parse each string into an int.
This way don't need to know number of elements beforehand but if it's necessary to get exactly n number of elements in list then you can use Iterable.take() method.
List<int> list1 = stdin
.readLineSync()
?.split(RegExp(r'\s+'))
.map((e) => int.parse(e))
.take(n)
.toList() ??
List<int>.empty();
List<int> l = [ 1, 2, 3, 4 ];
var b = ByteData(10);
May I know what is the easiest way to fill b (position 4 ~ 7) with data from l.
I can certainly iterate through l and then fill b one by one. But this is just part of a larger solution. So I hope there is a simpler way (for easy maintenance in future).
ByteData represent an area of memory counted in bytes but does not tell us anything how we want to represent the data inside this block of memory.
Normally, we would use one of the specific data types from dart:typed_data like e.g. Uint8List, Int8List, Uint16List and so on which have a lot more functionality.
But you can easily get the same by making a view over your ByteData. In this example I guess you want to insert your numbers as Uint8:
import 'dart:typed_data';
void main() {
List<int> l = [ 1, 2, 3, 4 ];
var b = ByteData(10);
var uInt8ListViewOverB = b.buffer.asUint8List();
uInt8ListViewOverB.setAll(4, l);
print(uInt8ListViewOverB); // [0, 0, 0, 0, 1, 2, 3, 4, 0, 0]
}
I recommend reading the documentation for the different methods on ByteBuffer (returned by buffer). You can e.g. make subview of a limited part of your ByteData if your ByteData needs to contain different types of data:
https://api.dart.dev/stable/2.15.0/dart-typed_data/ByteBuffer/asUint8List.html
Maybe I'm missing a keyword in my searches for a solution, but I didn't find what I'm looking for.
In Google Sheets I want to take a set of numbers and reorder it randomly. For example, start with the set [1,2,3,4] and get back [4,2,1,3].
Any ideas which function or a combination of functions may achieve this goal?
The entire process that I want to achieve is something like this:
I have a set of 4 fields. Their sum is fixed. I want to assign them randomized values.
So, I was thinking to iterate through this process:
Create a random integer between 0 and the max possible value (in the first iteration it's the fixed sum)
The new max value is the last max value minus the new random number.
Check if the new max is zero.
If not:
Return to the 1st step and repeat - This goes on until there are four values
If needed the 4th value shall be increased so the total will match the fixed sum.
Else, continue.
Randomize the order of the 4 values.
Assign the values to the 4 fields.
try:
=INDEX(SORT({{1; 2; 3; 4}, RANDARRAY(4, 1)}, 2, ),, 1)
or:
=INDEX(SORT({ROW(1:4), RANDARRAY(4, 1)}, 2, ),, 1)
Here are a couple of app script examples as well
function DiceRolls(nNumRolls) {
var anRolls = [];
nNumRolls = DefaultTo(nNumRolls, 1000)
for (var i = 1;i <= nNumRolls; i++) {
anRolls.push(parseInt((Math.random() * 6))+1);
}
return anRolls;
}
function CoinFlips(nNumFlips) {
var anFlips = [];
nNumFlips = DefaultTo(nNumFlips, 1000)
for (var i = 1;i <= nNumFlips; i++) {
anFlips.push(getRndInteger(1,2));
}
return anFlips;
}
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
How do I convert the below java code to equivalent dart.
private static final byte[] mIdBytes = new byte[]{(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x7E};
byte[] data;
System.arraycopy(mIdBytes, 2, data, 0, 4);
Is there any Dart method that does a similar kind of operation?
I was looking into this:
https://pub.dev/documentation/ckb_dart_sdk/latest/ckb-utils_number/arrayCopy.html
To match Java's System.arrayCopy(source, sourceOffset, target, targetOffset, length)
you should use
target.setRange(targetOffset, targetOffset + length, source, sourceOffset);
This is more efficient than using List.copyRange for some lists, for example copying between typed-data lists with the same element size (like two Uint8Lists).
Well, I found the way to do it.
you can just use
List.copyRange(data, 0, mIdBytes, 2);
This is a workaround I kinda found to be done in your case. This is called sublist(), this method will take the start index, and an end index.
IDEA:
Use sublist(), and copy the elements to be started from, that sourcePos = you_pos
Source array will be used like sourceArray.sublist(startIndext, endIndex)
The destination array will be initialized with the value using sublist()
Till what length the item should be added would be mentioned in the end index+2, since it will ignore the last item, and copy till the index-1
FINAL CODE
void main() {
List<int> source = [1, 2, 3, 4, 5, 6];
List<int> target = [];
int startPos = 1;
int length = 4;
// to ensure the length doesn't exceeds limit
// length+2 because, it targets on the end index, that is 4 in source list
// but the end result should be length+2 to contain a length of 5 items
if(length+1 <= source.length-1){
target = source.sublist(startPos, length+2);
print(target);
}else{
print('Cannot copy items till $length: index out of bound');
}
}
//OUTPUT
[2, 3, 4, 5, 6]
I want to convert number words (like one, two, three and so on) to int (like 1, 2, 3 etc) using Dart
You have to depend on machine learning library or pair every string with the respective number.
int convertStrToNum(String str) {
var number = <String, num>{'one': 1, ...};
return number[str];
}
Of course, machine learning might be the fastest and best way to do this, but I can't really help you there. So, here's an implementation that would work assuming the "number word" follows a certain format, up until 10. (You could implement a RegExp parser to make this easier, but that would get tricky).
int convStrToNum(String str) {
var oneten = <String, num> {
'one': 1,
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine': 9,
'ten': 10,
}
if (oneten.keys.contains(str)) {
return oneten[str];
}
}
int convStrToInt(String str) {
var list = [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
];
return list.indexOf(str);
}
I just pushed a repo addressing this issue. It's open to open to build upon so feel free to contribute, it would really help improve the package. Heres the link https://github.com/michaelessiet/wordstonumbers.dart